简体   繁体   English

415 Spring MVC和Rest Service不支持的媒体类型

[英]415 Unsupported Media Type with Spring MVC and Rest Service

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: 我的Angular Services.js:

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: 我的controller.js中的代码可以调用:

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! 谢谢!

Why is your consumes parameter set to "" ? 为什么将consumes参数设置为""

If: 如果:

  • you delete consumes = "", from your mapping 您从映射中删除了consumes = "",
  • JSON is properly configured in your app (the defaults should be fine) 在您的应用中正确配置了JSON(默认值可以)
  • your client application sends the proper Content-Type HTTP Header 您的客户端应用程序发送了正确的Content-Type HTTP标头

Then it should work. 然后它应该工作。

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) {

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. 在网络选项卡中查看,首先您需要确认Angular是否正在发送url中的参数,可能您的请求正在发送有关请求有效负载的信息。

The error 415 is erro to convert information. 错误415错误地转换了信息。 @PathVariable is a annotation to get parameter inside the url: @PathVariable是在URL内获取参数的注释:

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

Create an object and insert it in the method using a annotation @RequestBody 创建一个对象,并使用注释@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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM