Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 1.2 KB

2015-09-21-perform-update-with-spring-data-rest-and-angular.md

File metadata and controls

40 lines (34 loc) · 1.2 KB
layout title date comments categories description tags
post
Perform an update using Angular's $resource and Spring Data REST
2015-09-21 02:26:45 -0700
true
spring data rest
angular js

Using Spring Data REST with Angular's $resource object is very useful and convenient, but I've faced a problem with CRUD update. First you need to create such method while creating $resource object:

{% highlight javascript %} angular.module('myApp.services',[]).factory('Something',function($resource){ return $resource('http://giveMeSomeRest.nom/please/:id',{id:'@_id'},{ update: { method: 'PUT' } }); }); {% endhighlight javascript %}

But when this method was called I got 405 Method Not Allowed error. The problem is Spring expects to have an id in the URL. To add it modify creation of a $resource object:

{% highlight javascript %} angular.module('myApp.services',[]).factory('Something',function($resource){ return $resource('http://giveMeSomeRest.nom/please/:id',{id:'@_id'},{ update: { method: 'PUT', params: {id: "@id"}, } }); }); {% endhighlight javascript %}

Please note that @id refers to the parameter defined above, so no underscore is needed.