【问题标题】:415 Unsupported Media Type with Spring MVC and Rest Service415 不支持的媒体类型与 Spring MVC 和 Rest 服务
【发布时间】:2014-05-25 09:59:00
【问题描述】:

我来了

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

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

【问题讨论】:

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


    【解决方案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】:

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

      如果:

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

      那么它应该可以工作了。

      【讨论】:

      • 对不起,没有消耗它也不能工作,我只是添加它来检查是否有效!没有它也无法正常工作。
      【解决方案3】:

      查看网络选项卡,首先您需要确认 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;
      

      }

      【讨论】:

      • 不,更改为@RequestBody 无效;有问题我已经提到键值是在上面显示的 localhost url 中传递的。这是响应标头: Content-Language en Content-Length 949 Content-Type text/html;charset=utf-8 Date Fri, 11 Apr 2014 16:05:27 GMT Server Apache-Coyote/1.1 Request Headers Accept application/json, text/plain, / Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5
      猜你喜欢
      • 2013-08-08
      • 2023-03-04
      • 2017-05-20
      • 2014-11-04
      • 2014-09-15
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多