Spring's Json not being resolved with appropriate response

34,977

Solution 1

Get rid of all Jackson beans, and of the json mapping in the negotiating resolver. the mvc:annotation-driven should configure everything you need for the Jackson serialization to work.

Solution 2

  1. Make sure the POJO you return has get()ers, one for each field.
  2. Make sure the appserver (Tomcat) has the libraries even if you are sure your build system (Eclipse/Maven) does.

I've had this error twice now. Just now I added getters to my pojo. The 406 error went away and I got JSON as expected. I assume that because my fields were package-protected (the default access), it would grab them, but I guess not. For the record, in case it matters, I also made the POJO implement Serializable, toString(), serialVersionUID, no-arg constructor, and explicit constructors.

The prior time I cleaned/cleared/refreshed my Tomcat cache and did whatever else to force it to reload. I believe when I added the Jackson dependencies, it fixed my compile time errors, but since tomcat missed them, at runtime Spring MVC did not discover the Jackson libraries, and produced the 406 error about unacceptable response type.

Solution 3

I have got same exception when migrating from spring 3.x to spring 4.x.

I solved it with updating jackson dependencies from

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

to

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

Nothing else was needed for me.

Solution 4

Also, make sure that you add two jackson related jar files.

  • jackson-core-asl-1.9.8.jar
  • jackson-mapper-asl-1.9.8.jar

The version can be different.

Solution 5

I know that this is an old thread, but maybe someone will encounter the same problem as me. I got that exception when migrating an application from Spring 4.0.3.RELEASE to Spring 4.1.0.RELEASE. In my case updating Jackson from 1.9.x to 2.4.x did the trick.

Share:
34,977

Related videos on Youtube

ClutchDude
Author by

ClutchDude

I'm a Java/.Net developer who works on web-apps.

Updated on January 10, 2020

Comments

  • ClutchDude
    ClutchDude over 4 years

    I've tried to have a controller in Spring return a JSON response to no avail using the Jackson classes as recommended with 3.0. I've got the jackson jar files(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar) in my class path of course.

    As for the appconfig.xml entries, I'm not sure I need these. I've put them in there as a last act of desperation before returning to ol' fashion non-json ajax.

    In debug, I watch the controller get the request, return the foo and then, in firebug, get a 406.

    The error messages are as follows: From the logger when set to debug: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

    From the response: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

    My appconfig.xml is here:

        <!-- Configures support for @Controllers -->
        <mvc:annotation-driven />
    
        <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
          <entry key="html" value="text/html"/>
          <entry key="json" value="application/json"/>
        </map>
      </property>
      <property name="viewResolvers">
        <list>
          <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
          </bean>
        </list>
      </property>
      <property name="defaultViews">
        <list>
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
      </property>
    </bean>
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"></property>
        </bean>
    

    My controller

    @RequestMapping(value="foo/bar", method=RequestMethod.GET)
    public @ResponseBody foo getFoo(@RequestParam String fooId) {
        return new foo(fooId); 
    }
    

    On the jsp, where the ajax call is made:

    function addRow() {
      $.getJSON("foo/bar", {
        fooId: 1
      }, function(data) {
        alert("it worked.");
      });
    }
    

    Let me know if there's any more info that is needed.

  • stevebot
    stevebot over 13 years
    I tried this too, but it didn't work for me. Any ideas? I have Jackson on my classpath, and I'm using a plain old object but it still doesn't seem to cut it.
  • Ithar
    Ithar over 9 years
    What about using produces="application/json" on the @RequestBody annotation.