Sharing Top Content from the Angular-sphere.

Spring Rest Controller with angularjs resource

  • Angularjs ngResource is an angularjs module for interacting with REST based services.
  • I used it recently for a small project with Spring MVC and wanted to document a configuration that worked well for me.
  • The controller is run of the mill, it supports CRUD operations on a Hotel entity and supports the following methods: – –   – –   – –   – –   – – – – This can implemented in the following way using Spring MVC: – – Note the…
  • On the angularjs side, the ngResource module can be configured in a factory the following way, to consume this service: – – The only change to the default configuration is in specifying the additional “update” action with the Http method of PUT instead of POST.
  • With this change, the REST API can be accessed the following way: – – Or another variation of this: – – To handle successful and failure outcomes just pass in additional callback handlers:

Angularjs ngResource is an angularjs module for interacting with REST based services. I used it recently for a small project with Spring MVC and wanted to document a configuration that worked well for me.

Angularjs ngResource is an angularjs module for interacting with REST based services. I used it recently for a small project with Spring MVC and wanted to document a configuration that worked well for me.

The controller is run of the mill, it supports CRUD operations on a Hotel entity and supports the following methods:

This can implemented in the following way using Spring MVC:

Note the @RestController annotation, this is a new annotation introduced with Spring Framework 4.0, with this annotation specified on the controller, the @ResponseBody annotation on each of the methods can be avoided.

On the angularjs side, the ngResource module can be configured in a factory the following way, to consume this service:

app.factory(“Hotel”, function ($resource) { return $resource(“/rest/hotels”, {id: “@id”}, { update: { method: ‘PUT’ } }); });

The only change to the default configuration is in specifying the additional “update” action with the Http method of PUT instead of POST. With this change, the REST API can be accessed the following way:

POST /rest/hotels translates to:

Or another variation of this:

Hotel.save({}, {name:”test”,address:”test address”, zip:”0001″});

GET /rest/hotels translates to :

Hotel.query();

GET /rest/hotels/:id translates to :

PUT /rest/hotels/:id translates to :

var hotel = new Hotel({id:1, name:”test”,address:”test address”, zip:”0001″}); hotel.$update();

DELETE /rest/hotels/:id translates to:

To handle successful and failure outcomes just pass in…

Spring Rest Controller with angularjs resource

Comments are closed, but trackbacks and pingbacks are open.