Java 如何在 Spring 4 中使用 fastxml Jackson JSON 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23330492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use fasterxml Hymanson JSON serialization in Spring 4
提问by John O'Conner
Why doesn't my Spring controller return media type application/json
when I call it with the following curl:
application/json
当我使用以下 curl 调用它时,为什么我的 Spring 控制器不返回媒体类型:
curl -v -i -H "Accept: application/json" localhost:8080/properties-ws/prop/2322
I've read all other questions and responses on this subject and none address my specific issue.
我已经阅读了关于这个主题的所有其他问题和回复,但没有一个解决我的具体问题。
I'm using Spring 4, and have a simple Controller and pom.
我正在使用 Spring 4,并且有一个简单的 Controller 和 pom。
Controller method
控制器方法
@RequestMapping(value="/prop/{character}",
method=RequestMethod.GET,
produces={MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody CharData getData(@PathVariable(value="character") int codePoint) {
CharData chData = null;
chData = CharPropertiesService.getProperties(codePoint);
return chData;
}
POM file with the dependency on Hymanson
依赖于 Hymanson 的 POM 文件
I included the fasterxml Hymanson-databind artifact version 2.3.3
我包括了fasterxml Hymanson-databind artifact version 2.3.3
Here is the pom:
这是pom:
<?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.joconner.unicode</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>properties-ws</name>
<artifactId>properties-ws</artifactId>
<packaging>war</packaging>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.3.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.9</org.aspectj-version>
<org.slf4j-version>1.6.1</org.slf4j-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.joconner.unicode</groupId>
<artifactId>properties</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.3.3</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Spring Web MVC Features -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 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>
</dependencies>
</project>
The controller returns HTTP response 406 with a message:
控制器返回带有消息的 HTTP 响应 406:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应。
Any ideas about where I'm not configuring this properly?
关于我没有正确配置的任何想法?
采纳答案by John O'Conner
Thanks to Sotirios, I've resolved my issue. I'm not sure why this resolved it, but it did.
感谢 Sotirios,我解决了我的问题。我不确定为什么这解决了它,但确实如此。
I simply put the following in my servlet-context.xml:
我只是将以下内容放在我的 servlet-context.xml 中:
<mvc:annotation-driven/>
That's it. That's all it took, and now Spring is returning the JSON representation of object that I expected.
就是这样。这就是全部,现在 Spring 正在返回我期望的对象的 JSON 表示。