Spring MVC 4 REST Web服务获取错误406

时间:2015-03-15 17:17:43

标签: rest spring-mvc jackson spring-4 http-status-code-406

我遇到了这部分代码,我收到HTTP 406错误。

  

此请求标识的资源只能根据请求“accept”标题生成具有不可接受特征的响应。


我搜索了两天,尝试了各种可能性,无法找出解决方案。我使用的服务器是tomcat8。
这是我的控制器类:

@RestController
public class WebServiceController {


@RequestMapping(value="/testpage",method=RequestMethod.GET)
public TestObject test(){
    TestObject to = new TestObject();
    to.setAge("er");
    to.setName("sdfsdf");
    return to;
}
}

我的TestObject:

public class TestObject {

private String name;
private String age;
public String getName() {
    return name;    }
public void setName(String name) {
    this.name = name;   }
public String getAge() {
    return age; }
public void setAge(String age) {
    this.age = age; }
}

我的调度员服务员

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"           
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.kpos.webservice"/>

</beans> 

这是我的pom.xml

<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.kpos.play.ws</groupId>
<artifactId>CFBService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>CFBService Maven Webapp</name>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <finalName>CFBService</finalName>
</build>

  

我读的就好像我们使用@RestController注释一样,它会在classpath&amp;中使用jackson自动将对象转换为JSON。 @RestController自动处理@ResponseBody。

1 个答案:

答案 0 :(得分:1)

尝试将所有杰克逊图书馆作为以下博客:

http://www.breathejava.com/restful-web-service-tutorial-spring-xml/

问题与缺少杰克逊图书馆有关。