Вопрос

I am getting

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 is:

http://localhost:8080/ngdemo/web/posts/review/80a5d7660cdb82a8ef9f8db79bb3c8ab14555377

error while reading from spring controller; I checked with my other controller methods of same pattern and they are working fine but not this one which I newly introduced. I cant find any issue with it, can you please suggest whats I am 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 of 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'}
    })};

}]);

Code in my controller.js which makea call:

var reviewId = place.id;
$scope.allreviews = PostFactory.reviews.query({key: reviewId})

I cant find where the issue is, so can you guys please have a look and point me what is that which I missed? Thanks!

Это было полезно?

Решение 2

It 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 consumes parameter set to ""?

If:

  • you delete consumes = "", from your mapping
  • JSON is properly configured in your app (the defaults should be fine)
  • your client application sends the proper Content-Type HTTP Header

Then it should work.

Look in the network tab, first you need to confirm if Angular are sending the parameter in the url, probably your request are send the information on the request payload.

The error 415 is erro to convert information. @PathVariable is a annotation to get parameter inside the url:

https://stackoverflow.com/{pathVariableParam}/

Create an object and insert it in the method using a annotation @RequestBody

@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;

}

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top