我正在开发一个Spring3.2MVC应用程序。它的设置相当典型,但我的问题是,当我试图从我的控制器方法返回一个JSON对象时,我得到了一个406状态错误。
令人沮丧的是,除了我的一个方法之外,我从服务器得到了所有的响应。我包含的代码是我在web上找到的一个修改后的Spring MVC json示例。使用path变量的示例控制器方法可以按预期工作,但我添加到控制器中的其他方法却不能。
我对示例代码所做的唯一其他更改发生在pom中。我将jackson依赖项升级到了最新版本。
那么我错过了什么呢?我的类路径中有jackson映射器罐子。我在servlet中使用<mvc:annotation-driven />指令,并在每个控制器方法中使用@ResponseBody注释。
任何帮助都将不胜感激!
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>
控制器:
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;
}
}发布于 2013-05-25 21:15:47
不起作用的方法的映射地址以.html结尾。这没有任何意义,因为您返回的是JSON数据。我建议您更改映射,如下所示:
@RequestMapping(value="/testJson.json", method=RequestMethod.GET)由于该扩展名,Spring将请求的媒体类型设置为html,因此不会将其转发到JSON视图。
发布于 2013-11-21 18:40:31
这可能是你的mvc的一个问题:在3.2中引入的注解已经在这里得到了回答:
https://stackoverflow.com/a/13939290/3014094
mvc还有一个额外的变化:在Spring3.2中需要注解。它试图根据请求的扩展名自动分配媒体类型。我猜很可能是/doSomething.html,它没有映射到JSON。
更新mvc spring配置以关闭此自动映射。
<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>https://stackoverflow.com/questions/16732621
复制相似问题