406 Spring MVC Json,根据请求“接受”标头不可接受

Posted

技术标签:

【中文标题】406 Spring MVC Json,根据请求“接受”标头不可接受【英文标题】:406 Spring MVC Json, not acceptable according to the request "accept" headers 【发布时间】:2014-12-24 06:37:09 【问题描述】:

在我的 pom.xml 中有以下详细信息

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

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>3.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$spring-framework.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>

应用程序配置.xml:

<context:component-scan base-package="com.test" />
<mvc:annotation-driven />
<!-- <mvc:default-servlet-handler /> -->
<mvc:resources mapping="/resources/**" location="/resources/" />

JSP 页面:

 <form:form method="POST" action="/QuickBooks-UX/syncAccounts">
        <input type="submit" value="Sync Account"/>
 </form:form>

控制器:

@Controller
@RequestMapping("/")
public class QuickBooksController 
    @RequestMapping(value = "/quickBooks", method = RequestMethod.GET)
    public String qucikBooks(ModelMap model) 
        logger.info("Welcome to QuickBooks controller");
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "quickBooks";
    
    @RequestMapping(value ="/syncAccounts", method = RequestMethod.POST)
    public @ResponseBody List<SyncData> syncAccounts(@ModelAttribute("syncData")SyncData syncData, ModelMap model, BindingResult result) 
          List<SyncData> syncDataList = new ArrayList<SyncData>();
          try 

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet getRequest = new HttpGet(
                    "http://localhost:8292/qbsyncdata/getAccounts");
                getRequest.addHeader("accept", "application/json");

                HttpResponse response = httpClient.execute(getRequest);
                if (response.getStatusLine().getStatusCode() != 200) 
                    throw new RuntimeException("Failed : HTTP error code : "
                       + response.getStatusLine().getStatusCode());
                
                BufferedReader br = new BufferedReader(
                                 new InputStreamReader((response.getEntity().getContent())));                   
                while ((output = br.readLine()) != null) 
                    JSONParser jsonParser = new JSONParser();
                    JSONArray jsonArray = (JSONArray)jsonParser.parse(output);
                    for (Object object : jsonArray) 
                        JSONObject jsonObject = (JSONObject)object;
                        syncData = new SyncData();
                        syncData.setAccountName(jsonObject.get("accountName")==null?"":jsonObject.get("accountName").toString());
                        syncData.setAccountType(jsonObject.get("accountType")==null?"":jsonObject.get("accountType").toString());
                        syncData.setAccountSubType(jsonObject.get("accountSubType")==null?"":jsonObject.get("accountSubType").toString());
                        syncData.setActive(jsonObject.get("active")==null?"":jsonObject.get("active").toString());
                        syncDataList.add(syncData);
                                       
                    model.addAttribute("syncData", output);
                
                httpClient.getConnectionManager().shutdown();
               catch (Exception e) 
                e.printStackTrace();
               
            
         return syncDataList;
    

我将我的网址调用为:

http://lt-50k7sy1:8080/QuickBooks-UX/quickBooks

点击按钮后,返回的 url 为 http://lt-50k7sy1:8080/QuickBooks-UX/syncAccounts 这返回 406 并且描述是:

此请求标识的资源只能生成 具有根据请求不可接受的特性的响应 “接受”标题

我关注了这个Link,但没有结果。

【问题讨论】:

遇到类似问题,关注***.com/questions/26416248/… 【参考方案1】:

它适用于我的 RestController 中的以下依赖项和 @EnableWebMvc,请注意我刚刚添加了 jackson-databind 依赖项。

  <properties>
    <spring-version>4.2.1.RELEASE</spring-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>$spring-version</version>  
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>$spring-version</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>$spring-version</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.3</version>
    </dependency>
  </dependencies>

【讨论】:

【参考方案2】:

也许你的 POJO 的所有字段都需要 Getter 和 Setter。

我根据这个问题修复了: 参考:Spring MVC - HttpMediaTypeNotAcceptableException

而且 406 不是修复错误的有用消息。 您应该调试代码并查看异常到底是什么。

【讨论】:

【参考方案3】:

在使用 spring 4 的 pom 中使用以下依赖项。

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

【讨论】:

【参考方案4】:

通过在 Controller 类中添加 @EnableWebMvc 来消除 this 问题。

@Controller
@RequestMapping("/API/course")
@EnableWebMvc
public class CourseController 
 @Autowired
 private com.item.DAO.CourseRepository courseRepository;

 @ResponseStatus(value=HttpStatus.OK)
 @RequestMapping(method=RequestMethod.GET)
 public @ResponseBody List<Course> getListOfCourse()
    List<Course> courses = courseRepository.getListOfCourse();
    return courses ;

如果您的项目中正在使用 XML 配置,则在 xml 配置文件中添加以下行。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<bean>

并在您的 pom.xml 文件中添加以下依赖项:-

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1.3</version>
    </dependency>
    <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-annotations</artifactId>
        <version>2.4.1</version>
    </dependency>

【讨论】:

仅仅添加依赖项对我不起作用。 @EnableWebMvc 在这里丢失了。谢谢! WOW @EnableMvcController 解决了我的问题。太感谢了。但这太可怕了,我们在腐烂的 Spring 基础设施之上添加了一些“修复”/“黑客”,这真的很糟糕。我有一个正确的 RestController 结构,但它不起作用,只有这个“hack”有帮助。【参考方案5】:

谢谢它解决了我的问题。我最终得到了这样的 pom.xml:

<!-- Json dependency -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <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>
    <!-- Json dependency -->

【讨论】:

【参考方案6】:

将以下 jar 添加到 Spring 4.1 所需的 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>

【讨论】:

谢谢,很有帮助(我使用的是 2.4.3 版)。还发现 xpadro.blogspot.ca/2014/01/… 迁移指南很有帮助。

以上是关于406 Spring MVC Json,根据请求“接受”标头不可接受的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC + JSON = 406 不可接受

Spring4 MVC json问题(406 Not Acceptable)

SSM框架整合时ajax请求返回406

http请求后台报406错误

mvc:annotation-driven 不能解决 406 错误

jsckson,想说爱你不容易啊。。。406错误