2

I get following response when I try to delete: 405 Method Not Allowed. In my logs there is written that GET is allowed, but DELETE isn't.

Java:

   @ResponseBody
   @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
   public void delete(@PathVariable String id) {
     speakerService.delete(id);
   }

Angularjs

  app.factory('SpeakerResource', function ($resource) {
      return $resource('rest/speaker/:speakerId',
    {
        speakerId: '@speakerId'
    },
    {
        'update': { method: 'PUT' }
    },
     {
         'delete': { method: 'DELETE', params: { 'id': 'speakerId' }}

     }
    )
 });

SpeakerService

 this.delete = function (id, callback) {

    SpeakerResource.delete({ speakerId: id }, function () {
        callback();
    });

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
m2rt
  • 157
  • 2
  • 12

2 Answers2

4

I do not know your complete code, and I am not an expert in AngularJS, but it looks like your want to send a DELETE request to the URL <hopefullySomething>/{id} (Path variable). But it looks like that you send a DELETE request so some URL with an parameter id <hopefullySomething>?id={id} (Request parameter).

This question and answers explain the difference between path variable and request parameters a bit more @RequestParam vs @PathVariable

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
3

use $http.delete(), and return data for example status, I just tested the following with spring and working correctly

@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)  
     public @ResponseBody  Status deletePerson(@PathVariable("id") int id) {      
          try { 
              personService.removePerson(id); 
           return new Status(1, "person deleted Successfully !");  
          } catch (Exception e) {  
           return new Status(0, e.toString());  
          }       
     }

angular

angular.module('personService', [])

.factory('Person', ['$http',function($http) {
    return {

        deletePerson: function(id) {
            return $http.delete('/restperson/delete/'+id);
        }
    }
}]);

controller

angular.module('personController', [])

// inject the person service factory into our controller
.controller('mainController', ['$scope','$http','Person', function($scope, $http, Person) {     

    //delete
    $scope.deletePerson = function(id) {
        Person.deletePerson(id)
            .success(function(data) {
                $scope.message = data; 
            });
    };

}]);
Edy Aguirre
  • 2,113
  • 20
  • 20