Spring JSON请求:HTTP状态406

时间:2013-02-20 17:45:03

标签: json spring 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>

jackson-mapper-asl-1.9.5.jarjackson-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是一个非常简单的演示类:

package com.mycompany.whatever;

public class GreeterDemo {
    private String name;

    public GreeterDemo(String name) {
        this.name = name;
    }

}

当我针对我的开发服务器(Tomcat v7.0)http://localhost:8080/whatever/demo/sayhello/SomeName发出GET请求时,我总是收到错误消息:

  

HTTP状态406 -

     

输入状态报告

     

消息

     

description此请求标识的资源仅能够   产生具有不可接受的特征的响应   请求“接受”标题。

     

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

任何想法......?

1 个答案:

答案 0 :(得分:5)

问题是GreeterDemo类型缺少getter(基于对问题的评论中的讨论),并且一旦添加了getter,Jackson拒绝接受它作为可以转换为json的有效类型转换为json按预期进行。