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

后端 未结 14 1447
太阳男子
太阳男子 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:39

    Thank you for sharing you experience.I experienced the same problem and it works for me using configuration as show below:

    1. Spring MVC Version : 3.2.5.RELEASE
    2. Apache-tomcat-6.0.24
    3. JDK1.6
    4. jackson-core-asl-1.9.10.jar
    5. jackson-mapper-asl-1.9.10.jar

    Spring MVC Config File:

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

    Model class : Country.java

     private Integer countryId;
     private String name;
     //public setters and getters
    

    Controller Method:

    @RequestMapping(value = "/get_country_json",method = RequestMethod.GET)
    @ResponseBody
    public Country getCountry()
    

    Deployment Descriptor(web.xml)

     <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>       
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    

    URL Requested to call controller method: /SpringCURDApp/get_country_json.htm

    I hope this can help someone.

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

    I had the same problem and I solved it by adding following dependency

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    

    Previously I'm doing it with following dependency

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${com.jackson.core-version}</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${com.jackson.core-version}</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${com.jackson.core-version}</version>
    </dependency>
    

    In short I have replace com.fasterxml.jackson.core by org.codehaus.jackson

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