我正在开发一个 Spring 3.2 MVC 应用程序。它的设置相当典型,但我的问题是当我尝试从控制器方法返回 JSON 对象时收到 406 状态错误。
令人沮丧的部分是,除了我的一种方法之外,我还从服务器获得了响应。我包含的代码是我在网上找到的修改后的 Spring MVC json 示例。带有路径变量的示例控制器方法按预期工作,但我添加到控制器的其他方法没有。
我对示例代码所做的唯一其他更改发生在 pom.xml 中。我将杰克逊依赖升级到最新版本。
那么我错过了什么?我的类路径上有杰克逊映射器罐子。我<mvc:annotation-driven />
在我的 servlet 中使用该指令,并且在我的@ResponseBody
每个控制器方法中使用注释。
任何帮助将不胜感激!
调度员小服务程序:
<?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>
控制器:
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;
}
}
人.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;
}
}