11

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 Spring release 4.1.1(as of 10/16) with no any other configuration changes. Is this considered a bug or the 4.1.1 requires different configuration?

the latest jackson jar is already in the classpath

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

The example on the Spring document works fine

@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
  return "Hello World";
}

when the return type is String. The problem happens when the return type is a POJO.

Ralph
  • 118,862
  • 56
  • 287
  • 383
Dino Tw
  • 3,167
  • 4
  • 34
  • 48

3 Answers3

17

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>
Vito
  • 1,080
  • 9
  • 19
  • After I added `jackson-databind`(and the only extra jar I added, which is not required prior to 4.1.1 RELEASE), it works now. To wrap up, in order for @ResponseBody to work on Spring 4.1.1, I have `jackson-mapper-asl` and `jackson-databind` jars and `` in the config file. Thanks for the hint. – Dino Tw Oct 17 '14 at 04:01
  • Why would you include two versions of Jackson? – a better oliver Oct 17 '14 at 10:08
  • @DinoTw you can read this post:https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework#enforced-minimum-dependency-versions – Vito Jan 07 '15 at 09:45
  • @Vito, thanks for the update. Found that the minimum jars required for `@ResponseBody` to work are spring-webmvc and jackson-databind. – Dino Tw Jan 23 '15 at 00:13
  • @Vito You lost a dependence: jackson-annotaions. I tested it in spring 4.2.4, which required jackson-annotaions. – Rockystech Feb 22 '16 at 11:08
  • Thanks @Vito, this is what I was actually looking for. Thanks a lot buddy – Azam Khan Apr 13 '17 at 04:27
8

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

1

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>
Vlad
  • 1,077
  • 8
  • 8