mblPrgr
mblPrgr

Reputation: 139

JSON Mapping in my Spring MVC 3.2 Application Works for Only One of My Methods

I am working on a Spring 3.2 MVC application. It's setup fairly typically, but my problem is that I am getting a 406 status error when I attempt to return a JSON object from my controller methods.

The frustrating part is that I get that response from the server from all but one of my methods. The code I have included is a modified Spring MVC json example I found on the web. The sample's controller method with the path variable works as expected, but the additional methods that I added to the controller do not.

The only other change I made to the sample code occurs in the pom. I upgraded the jackson dependency to the latest version.

So what am I missing? I have the jackson mapper jars on my classpath. I'm using the <mvc:annotation-driven /> directive in my servlet and I'm using the @ResponseBody annotation with each of my controller methods.

Any help would be greatly appreciated!

Dispatcher servlet:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="uk.co.jeeni" />

    <mvc:annotation-driven />

    </beans>

pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
     xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<groupId>uk.co.jeeni</groupId>
<artifactId>spring-json</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>

<name>Spring 3 MVC JSON Example</name>

<properties>
    <spring.version>3.2.1.RELEASE</spring.version>
</properties>

<dependencies>
    <!--
      Jackson mapper is used by spring to convert
      Java POJOs to JSON strings.
    -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>

    <!-- START: Spring web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- END: Spring web -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
        </plugin>
  </plugins>
    <finalName>spring-mvc-json</finalName>
</build>

Controller:

    package uk.co.jeeni;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;


    @Controller
    public class JsonService {

    private static Map<String, Person> data = new HashMap<String, Person>();
    static{
    data.put("ADAM", new Person("Adam", "Davies", 42));
    data.put("JANE", new Person("Jane", "Harrison", 35));
    }

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody Person getPerson(@PathVariable String name){
    Person p = new Person(name, name, 105);//data.get(name.toUpperCase());
    return p;
    }


@RequestMapping(value="/testJson.html", method=RequestMethod.GET)
public @ResponseBody List<Person> testJson()
{
    List<Person> people = new ArrayList<Person>();

    for(int i=0;i<10;i++)
    {
        Person p = new Person("Firstname", "Lastname", i);
        people.add(p);
    }

    return people;
}

@ResponseBody
@RequestMapping(value="testJsonPojo.html", method=RequestMethod.GET)
public Person testJsonPojo()
{
    Person individual = new Person("Firstname", "Lastname", 105);

    return individual;
}
}

Person.java:

    package uk.co.jeeni;

    public class Person {
    private String firstName;
    private String lastName;
    private int age;

    public Person(String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    }

    public String getFirstName() {
    return firstName;
    }

    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
}

Upvotes: 2

Views: 5686

Answers (2)

PaulB
PaulB

Reputation: 115

It might be a problem with your mvc:annotations introduced in 3.2 that's been answered here:

https://stackoverflow.com/a/13939290/3014094

There's an additional change to mvc:annotations needed in Spring 3.2. It's trying to automatically assign the media type based on the extension of the request. I'd guess that it's likely to be /doSomething.html which is not mapping to JSON.

Update your mvc spring config to turn this auto-mapping off

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

Upvotes: 1

lunr
lunr

Reputation: 5279

The mapped URL's for the methods that are not working end with .html. That doesn't make sense because you are returning JSON data. I suggest you change the mapping like:

@RequestMapping(value="/testJson.json", method=RequestMethod.GET)

Because of the extension, Spring sets the request's media type to html, so it is not forwarded to a JSON view.

Upvotes: 3

Related Questions