mri*_*z_p 3 spring json jackson
我知道,这在stackoverflow上反复弹出.不幸的是,提供的解决方案都没有解决我的问
我正在尝试让我的服务器使用Jackson消息转换器以最简单的方式使用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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我的控制器:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
GreeterDemo 是一个非常简单的Demo类:
package com.mycompany.whatever;
public class GreeterDemo {
private String name;
public GreeterDemo(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
当我针对我的开发服务器(Tomcat v7.0)发出GET请求时,我http://localhost:8080/whatever/demo/sayhello/SomeName总是收到错误消息:
HTTP状态406 -
类型状态报告
信息
description此请求标识的资源只能根据请求"accept"标头生成具有不可接受特征的响应.
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
Run Code Online (Sandbox Code Playgroud)
分别
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
Run Code Online (Sandbox Code Playgroud)
有任何想法吗...?
问题是该GreeterDemo类型缺少getter(基于对问题的评论中的讨论)并且Jackson拒绝接受它作为可以转换为json的有效类型,一旦添加了getter,转换为json按预期进行.
| 归档时间: |
|
| 查看次数: |
3782 次 |
| 最近记录: |