415具有Spring MVC和Rest服务的不支持的媒体类型

时间:2014-04-11 09:55:41

标签: spring angularjs rest spring-mvc angularjs-scope

我正在

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

请求网址为:

http://localhost:8080/ngdemo/web/posts/review/80a5d7660cdb82a8ef9f8db79bb3c8ab14555377
从弹簧控制器读取时出现

错误;我检查了相同模式的其他控制器方法,它们工作正常但不是我新引入的那个。我找不到它的任何问题,你能否建议我错过什么?

我的控制器:

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

Angular的My 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'}
    })};

}]);

我的controller.js中的代码打电话:

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

我无法找到问题所在,所以你们可以看看并指出我错过了什么?谢谢!

3 个答案:

答案 0 :(得分:1)

为什么您的consumes参数设置为""

如果:

  • 从地图中删除consumes = "",
  • 在您的应用中正确配置了JSON(默认设置应该没问题)
  • 您的客户端应用程序发送正确的Content-Type HTTP标头

然后它应该工作。

答案 1 :(得分:1)

它的工作原理是:

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

答案 2 :(得分:0)

查看网络选项卡,首先需要确认Angular是否在URL中发送参数,可能您的请求是发送有关请求有效负载的信息。

错误415是错误的转换信息。 @PathVariable是一个在url中获取参数的注释:

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

使用注释@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;

}