1

I am implementing REST service using Spring. When i call delete function the request doesn't reach controller and from JS i receive error: angular.js:12587 DELETE http://localhost:8080/Guard_Server/restapi/sites?siteId=5 500 (Internal Server Error)

my controller function looks like:

@RequestMapping(value="sites", method=RequestMethod.DELETE)
boolean deleteSite(@PathVariable("siteId") int site_id, HttpServletResponse res){
    .......
}

And AngularJS function is:

$scope.deleteSite=function(){
            if(confirm("Are you sure you want to delete this Site?")){
                var site={};
                site.siteId=sites[$scope.selectedSite].id;
                console.log(site);
                siteFactory.delete(site).$promise.then(function(result){
                    ...
                }
                ,error_on_execute);
            }
        }

with factory:

serverApp.factory('referentFactory',['$resource',function ($resource){
   return $resource("http://localhost:8080/Guard_Server/restapi/referents",null,{ 'update': {method: 'PUT'} });
}]);
AlexP
  • 449
  • 2
  • 9
  • 25
  • why don't add `@ResponseBody` to `deleteSite` function and why this function return primitive not object. also `$resource` url not match with `RequestMapping` – Hadi J Apr 30 '17 at 09:10
  • URL Matches as for ControllerClass stands `@RestController @RequestMapping("/restapi/")`, other methods work, so the problem is not URL. `@RequestBody` instead of `@PathVariable` doesn't resolve the problem,already tried – AlexP Apr 30 '17 at 09:18
  • if use `@RequestMapping(value="sites", method=RequestMethod.DELETE) boolean deleteSite(@RequestBody int siteId, HttpServletResponse res)` the error changes to 400 BAD Request – AlexP Apr 30 '17 at 09:21
  • 3
    as i see in your url you should use `@RequestParam` – Hadi J Apr 30 '17 at 09:21
  • `@RequestParam` worked! but what is the difference between `@RequestPath` and `@RequestParam`? – AlexP Apr 30 '17 at 09:24
  • @Hadi If you will post this as solutio I will accept it – AlexP Apr 30 '17 at 09:26

1 Answers1

0

As I see in the url

http://localhost:8080/Guard_Server/restapi/sites?siteId=5

you should use @RequestParam instead of @PathVariable.

@RequestParam use for access query parameter and @PathVariable is part of url.

@RequestParam:

   http://localhost:8080/Guard_Server/restapi/sites?siteId=5

@PathVariable

   http://localhost:8080/Guard_Server/restapi/sites/siteId/5

see this for more information

Community
  • 1
  • 1
Hadi J
  • 16,989
  • 4
  • 36
  • 62