415 Unsupported Media Type with Spring MVC and Stop Service
I get
415 Unsupported Media Type - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
Request url:
http://localhost:8080/ngdemo/web/posts/review/80a5d7660cdb82a8ef9f8db79bb3c8ab14555377
error while reading from spring controller; I have tested my other controller methods of the same template and they work fine, but not the one I recently introduced. I can't find any problem with it, can you suggest what I'm missing?
My controller:
@RequestMapping(value = "/review/{key}", method = RequestMethod.GET, consumes = "", produces = "application/json")
public
@ResponseBody
List<Review> reviews(@PathVariable(value = "key") String key) {
System.out.println("key : " + key);
List<Review> reviewList = reviewService.getReviewsById(key);
System.out.println("reviewList : " + reviewList.size());
return reviewList;
}
My Services.js Angular:
services.factory('PostFactory', ['$resource', function ($resource) {
alert("I am here service");
return {
postmain: $resource('/ngdemo/web/posts', {}, {
query: {method: 'GET', isArray: true },
create: {method: 'POST'}
}),
reviews: $resource('/ngdemo/web/posts/review/:key', {}, {
query: {method: 'GET', params: {key: '@key'} },
create: {method: 'POST'}
}),
postreview: $resource('/ngdemo/web/posts/getreview', {}, {
query: {method: 'GET', isArray: true },
create: {method: 'POST'}
}),
allresults: $resource('/ngdemo/web/posts/result/:tag', {}, {
query: {method: 'GET', params: {tag: '@tag'} },
create: {method: 'POST'}
})};
}]);
The code in my .js controller that makes the call:
var reviewId = place.id;
$scope.allreviews = PostFactory.reviews.query({key: reviewId})
I can't seem to find where the problem is, so guys, please take a look and point me to what I missed? Thank!
Worked by adding:
@Consumes("text/html")
@Consumes("text/html")
@RequestMapping(value = "/review/{key}", method = RequestMethod.GET, produces = "application/json")
public
@ResponseBody
List<Review> reviews(@PathVariable(value = "key") String key) {
Why is your parameter consumes
set to ""
?
If a:
- you remove
consumes = "",
from your mapping - JSON is configured correctly in your application (defaults should be accurate)
- your client application is sending the correct
Content-Type
HTTP header
Then it should work.
Look at the tab on the net, first you need to confirm if the Angular parameter is sending the parameter to the url, maybe your request is sending the request payload information.
Error 415 is an erro for converting information. @PathVariable is an annotation for getting a parameter inside a URL:
/fooobar.com / ... {pathVariableParam} /
Create an object and insert it into the method using @RequestBody annotation
@RequestMapping(value = "/review", method = RequestMethod.GET, consumes = "", produces = "application/json")
public
@ResponseBody List<Review> reviews(@RequestBody String key) { // Or (@RequestBody ObjectKey key)
System.out.println("key : " + key);
List<Review> reviewList = reviewService.getReviewsById(key);
System.out.println("reviewList : " + reviewList.size());
return reviewList;
}