本文介绍了如何在Spring 4中使用rapidxml Jackson JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why doesn't my Spring controller return media type application/json when I call it with the following curl:

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.

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

I'm using Spring 4, and have a simple Controller and 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;
}

依赖杰克逊的POM文件

我包括了fastxml jackson-databind工具版本2.3.3

I included the fasterxml jackson-databind artifact version 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并显示一条消息:

The controller returns HTTP response 406 with a message:

关于我没有正确配置的任何想法?

Any ideas about where I'm not configuring this properly?

推荐答案

感谢Sotirios,我已经解决了我的问题。我不确定为什么这会解决它,但确实如此。

Thanks to Sotirios, I've resolved my issue. I'm not sure why this resolved it, but it did.

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

I simply put the following in my servlet-context.xml:

   <mvc:annotation-driven/>

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

That's it. That's all it took, and now Spring is returning the JSON representation of object that I expected.

这篇关于如何在Spring 4中使用rapidxml Jackson JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:17