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.

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

6 Comments

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 <mvc:annotation-driven /> in the config file. Thanks for the hint.
Why would you include two versions of Jackson?
@Vito, thanks for the update. Found that the minimum jars required for @ResponseBody to work are spring-webmvc and jackson-databind.
@Vito You lost a dependence: jackson-annotaions. I tested it in spring 4.2.4, which required jackson-annotaions.
|
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 Comment

Thank you for the answer, will verify it later.
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.