在Spring 4中如何使用Fasterxml Jackson JSON序列化

application/json当我使用以下curl调用它时,为什么我的Spring控制器不返回媒体类型:

curl -v -i -H "Accept: application/json" localhost:8080/properties-ws/prop/2322

我已经阅读了有关此主题的所有其他问题和答案,但都没有解决我的特定问题。

我正在使用Spring 4,并且有一个简单的Controller和pom。

@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;

}

我包括了fasterxml jackson-databind工件版本2.3.3

这是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.jackson.core</groupId>

<artifactId>jackson-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>

控制器返回HTTP响应406并显示一条消息:

由该请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。

关于我在哪里配置不正确的任何想法?

回答:

感谢Sotirios,我已经解决了我的问题。我不确定为什么能解决这个问题,但是确实可以。

我只是将以下内容放入我的servlet-context.xml中:

   <mvc:annotation-driven/>

而已。这就是全部,现在Spring返回了我期望的对象的JSON表示形式。

以上是 在Spring 4中如何使用Fasterxml Jackson JSON序列化 的全部内容, 来源链接: utcz.com/qa/398635.html

回到顶部