Spring 4.1.1 RELEASE and @ResponseBody return HTTP 406

后端 未结 3 628
抹茶落季
抹茶落季 2020-12-10 13:27

I am using @ResponseBody to return Json object in Spring MVC. It works as expected on release 4.0.7 and 3.2.11, but it returns HTTP status 406 when I try to use the latest S

相关标签:
3条回答
  • 2020-12-10 14:07

    Got it working after removing Jackson 1.* replacing with the 2.4.4 (JAXRS), which will import all the other dependencies, jackson-core, jackson-databind and jackson-annotations.

    Removed

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

    Added

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
        <version>2.4.4</version>
    </dependency>
    

    http://www.codingpedia.org/ama/jquery-ui-autocomplete-not-working-in-spring-4-1/

    and in the servlet xml

    <mvc:annotation-driven  content-negotiation-manager="contentNegotiationManager" />
    
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
         <property name="favorPathExtension" value="false" />
         <property name="favorParameter" value="true" />
         <property name="mediaTypes" >
              <value>
                   json=application/json
                   xml=application/xml
              </value>
         </property>
    </bean>
    

    if the jackson annotate is imported in the class files, it should also be replaced

    Removed

    import org.codehaus.jackson.annotate.JsonIgnoreProperties

    Added

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties

    0 讨论(0)
  • 2020-12-10 14:23

    Maven pom.xml:

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

    and spring mvc config file (eg:spring-mvc.xml)

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    0 讨论(0)
  • 2020-12-10 14:33

    I was struggling with similar issue migrating from 3.2 to spring 4.2. Was getting

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type:

    posting it here so people can find it by exception name :) It took me half a day to find this article. Thanks @Vito and @Aias

    Combination of both previous answers work as well, if you do not want register custom contentNegotiationManager you can do following:

    remove all jackson dependencies from

    pom.xml

    and use latest one

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-base</artifactId>
        <version>2.6.1</version>
    </dependency>
    

    servlet.xml

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    0 讨论(0)
提交回复
热议问题