Spring 3.x JSON status 406 “characteristics not acceptable according to the request ”accept“ headers ()”

后端 未结 14 1446
太阳男子
太阳男子 2020-12-13 01:50

Upon trying to get my response in JSON using Spring 3.x, I get the 406 error \"The resource identified by this request is only capable

相关标签:
14条回答
  • 2020-12-13 02:30

    I have also just experienced this same issue. It would appear it is an issue with the latest 3.2.0.RELEASE, as I previously had 3.1.2.RELEASE and it all worked. After changing to 3.2.0.RELEASE it breaks. Have tested with 3.1.3.RELEASE and that works fine. So for now I would suggest rolling back to 3.1.3.RELEASE

    EDIT: Thanks to another post on this site that linked to the following location: http://static.springsource.org/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation

    I've now got it working by disabling the getting of media type based on the extension of the requested path. This can be done by the following:

    <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <!-- Turn off working out content type based on URL file extension, should fall back to looking at the Accept headers -->
        <property name="favorPathExtension" value="false" />
    </bean>
    

    And specify version 3.2 for all the xsd schema locations.

    And this is using the following jackson jars:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.1.2</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-13 02:30

    I had this problem in Spring MVC 4. Adding jackson-annotations, jackson-core and jackson-databind didn't solve the problem. Try this libs:

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-13 02:30

    I had a similar problem, it got resolved when I added jackson-databind library.

    These are my dependencies:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.3</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-13 02:33

    I had similar issue but it was weird. I will explain how I resolved it.

    In my web.xml my dispacher servlet was mapped to *.htm

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    and no matter what I did it always threw -

    The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" header

    Finally I changed it to

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    and it worked. What I think is when you just specify

    <mvc:annotation-driven/> 
    

    the extension takes precedence over accept header and .htm was was creating issues. And obviously I could not use xml or json since servlet itself was not mapped.

    I would also like to add that produces annotation that you, spring will try to map it will accept header of the incoming request. I was making request handler method generic for json and xml. Since I am using Java 8 and Jaxb is inbuilt in Java since Java 7 no need for JAXB dependency. For json I only needed to add -

    <dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.8.5"/>
    

    I am using ivy for dependency management.

    0 讨论(0)
  • 2020-12-13 02:34

    I had the same problem and the comment added by Biju Kunjummen in this answer worked for me perfectly

    https://stackoverflow.com/a/12873170/20654

    That is have public getters in my Java class

    0 讨论(0)
  • 2020-12-13 02:38

    Don't make the same mistake I did, spend all day playing around with Spring configuration, when actually your object returned in a web service is not marshaling to XML correctly. It seems Spring catches a JAXB marshaling error and doesn't report it. Use this sandbox code to validate JAXB marshaling:

    MyClass myclass = new MyClass();
    //populate myclass here
    
    JAXBContext context = JAXBContext.newInstance(MyClass.class);
    
    Marshaller m = context.createMarshaller();
    StringWriter w = new StringWriter();
    
    m.marshal(myclass, w);
    System.out.println(w);
    

    This produced and displayed an exception. Fixed the cause, and my web service is available in both XML and JSON.

    0 讨论(0)
提交回复
热议问题