【问题标题】:Spring 4, JSON - HTTP Error 406 Not AcceptableSpring 4,JSON - HTTP 错误 406 不可接受
【发布时间】:2015-03-17 16:19:06
【问题描述】:

我为 fullCalendar eventSource 创建了控制器,不幸的是出了点问题,每次我尝试打开此页面时都会收到 HTTP ERROR 406。

我的控制器:

@RequestMapping(value="/schedule", method = RequestMethod.GET)
public @ResponseBody Object getEventyJSON(Principal principal){
    String name = principal.getName();
    List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();

    List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();

    for (int i = 0; i < eventy.size(); i++) {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("title", eventy.get(i).getTitle());
        map.put("start", eventy.get(i).getStart());
        events.add(map);
    }

    return events;
}

我的 POM.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>

当然,我尝试将依赖项更改为 codehaus 而不是 fastxml,但它不起作用。我也尝试在方法中添加一些标题。

当我将控制器更改为此:

@RequestMapping(value="/schedule", method = RequestMethod.GET)
    public @ResponseBody Object getEventyJSON(Principal principal){
        String name = principal.getName();
        List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();


        return "schedule";
    }

它正在工作,当我打开该页面时,它会打印计划,所以它是正确的。 我错过了什么?

【问题讨论】:

    标签: java json spring maven model-view-controller


    【解决方案1】:

    我认为您的 pom.xml 中缺少依赖项

         <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    

    【讨论】:

      【解决方案2】:

      你的请求映射中需要这个

      @RequestMapping(value="/schedule", method=RequestMethod.GET, produces="application/json")
      

      你可能还需要这些依赖项

         <dependency>
              <groupId>org.codehaus.jackson</groupId>
              <artifactId>jackson-mapper-asl</artifactId>
              <version>1.9.13</version>
          </dependency>
      
          <dependency>
              <groupId>org.codehaus.jackson</groupId>
              <artifactId>jackson-core-asl</artifactId>
              <version>1.9.13</version>
          </dependency>
      

      【讨论】:

        【解决方案3】:

        尝试更改您的方法以返回一个字符串,并在您的方法中将您的列表转换为 JSON 字符串。为此,您可以使用 google-gson,但鉴于我们已经在使用 spring,并且可能已经在项目中拥有这些依赖项,您可以使用 Jackson 的 ObjectMapper。像这样:

        @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
        public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {
            // NOTE1: change the method to return a "String"
            // NOTE2: add "throws JsonProcessingException" to the method (or catch it)
            String name = principal.getName();
            List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();
            List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();
        
            for (int i = 0; i < eventy.size(); i++) {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("title", eventy.get(i).getTitle());
                map.put("start", eventy.get(i).getStart());
                events.add(map);
            }
            //return events;
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(events);
        }
        

        同时添加这两个导入:

        import com.fasterxml.jackson.core.JsonProcessingException;
        import com.fasterxml.jackson.databind.ObjectMapper;
        

        我很确定以上内容会为您解决问题。但是如果上述方法不能修复您的 406 错误响应,还有一件事可以尝试,请将其添加到 @RequestMapping:

        headers="Accept=*/*",produces = "application/json"
        

        例如:

        @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/js
        

        【讨论】:

          【解决方案4】:

          尝试更改您的方法以返回一个字符串,并在您的方法中将您的列表转换为 JSON 字符串。为此,您可以使用 google-gson,但鉴于我们已经在使用 spring,并且可能已经在项目中拥有这些依赖项,您可以使用 Jackson 的 ObjectMapper。像这样:

              @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
          public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {
                  // NOTE1: change the method to return a "String"
                  // NOTE2: add "throws JsonProcessingException" to the method (or catch it)
              String name = principal.getName();
              List<Eventy> eventy = userService.findOneWithEventy(name).getEventy();
              List<HashMap<String,String>> events = new ArrayList<HashMap<String,String>>();
          
              for (int i = 0; i < eventy.size(); i++) {
                  HashMap<String,String> map = new HashMap<String,String>();
                  map.put("title", eventy.get(i).getTitle());
                  map.put("start", eventy.get(i).getStart());
                  events.add(map);
              }
              //return events;
              ObjectMapper mapper = new ObjectMapper();
              return mapper.writeValueAsString(events);
          }
          

          同时添加这两个导入:

          import com.fasterxml.jackson.core.JsonProcessingException;
          import com.fasterxml.jackson.databind.ObjectMapper;
          

          我很确定以上内容会为您解决问题。但是如果上述方法不能修复您的 406 错误响应,还有一件事可以尝试,请将其添加到 @RequestMapping:

          headers="Accept=*/*",produces = "application/json"
          

          例如:

          @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")
          

          【讨论】:

            【解决方案5】:

            试试这个:

            @RequestMapping(value="/schedule", produces= "application/json")
            public @ResponseBody List<Eventy> getEventy(Principal principal){
            

            【讨论】:

              【解决方案6】:

              当服务器(您的后端)无法使用请求中指定的 Accept-Type HTTP 标头进行响应时,会发生 406。所以基本上你的服务器正在返回客户端请求Accept-Header中的不同响应类型。

              请求标识的资源只能生成响应实体,其内容特征根据请求中发送的accept headers是不可接受的。

              查看here

              【讨论】:

                【解决方案7】:

                在调用资源时,您需要告诉它您期望的内容类型

                如果你使用 JQuery,这样的东西会起作用

                $.ajax({
                    type: "GET",
                    url: "/schedule",
                    contentType: "application/json",
                    dataType : "json",
                    success: function (d) {
                        // do something with the json
                    }
                });
                

                【讨论】:

                • 我正在使用 FullCalendar 1.4 或 1.6,我得到了这个:事件:'/schedule',它应该可以工作。我已经按照您的方式更改了它,但仍然是 http 错误 406
                猜你喜欢
                • 1970-01-01
                • 2017-04-08
                • 2014-11-18
                • 1970-01-01
                • 2013-04-26
                • 2013-03-18
                • 2019-04-30
                • 1970-01-01
                相关资源
                最近更新 更多