我知道,这在stackoverflow上反复弹出。不幸的是,提供的解决方案都没有解决我的问题。
我试图让我的服务器使用杰克逊消息转换器以最简单的方式响应 JSON 格式的数据。
我的配置:
我的pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>whatever</artifactId>
<name>whatever</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
...
<!-- Jackson Mapper for JSON response -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.5</version>
</dependency>
...
</dependencies>
<build>
...
</build>
</project>
jackson-mapper-asl-1.9.5.jar
并且jackson-core-asl-1.9.5.jar
在构建路径上。
我的/whatever/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:component-scan base-package="com.mycompany.whatever" />
<annotation-driven />
...
</beans:beans>
我的控制器:
package com.mycompany.whatever;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@Controller
public class AParticularUserController implements UserController {
@RequestMapping(value = "/demo/sayhello/{name}", method = RequestMethod.GET)
public @ResponseBody GreeterDemo sayHello(@PathVariable String name) {
return new GreeterDemo(name);
}
}
GreeterDemo
是一个非常简单的Demo类:
package com.mycompany.whatever;
public class GreeterDemo {
private String name;
public GreeterDemo(String name) {
this.name = name;
}
}
当我对我的开发服务器(Tomcat v7.0)发出 GET 请求时,http://localhost:8080/whatever/demo/sayhello/SomeName
我总是收到错误消息:
HTTP 状态 406 -
类型状态报告
信息
描述 此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应。
Apache Tomcat/7.0.37
请求参数似乎是正确的:
Accept:application/json
Accept-Charset:UTF-8,*;q=0.5
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,de-DE;q=0.6,de;q=0.4,fr-FR;q=0.2,fr;q=0.2
Connection:keep-alive
Host:localhost:8080
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17
分别
Accept:*/*
Accept-Charset:UTF-8,*;q=0.5
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,de-DE;q=0.6,de;q=0.4,fr-FR;q=0.2,fr;q=0.2
Connection:keep-alive
Host:localhost:8080
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17
有任何想法吗...?