我正在使用Spring和Restful webservices开发一个基本的Hello World程序。但是,当我尝试拨打我的服务时,我收到以下错误消息:
HTTP状态406 -
description - 此请求标识的资源仅具有此功能 产生具有不可接受的特征的响应 请求“接受”标题。
这是我的web.xml文件的servlet映射:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
这是我的spring web配置文件:
<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"
xsi:schemaLocation="
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="hello" />
<mvc:annotation-driven />
<!-- Map returned view name "rssViewer" to bean id "rssViewer"
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
-->
<bean id="xmlViewer"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>hello.Greeting</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</beans>
这是我的Maven 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.examples</groupId>
<artifactId>WebServices1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WebServices1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- for compile only, your container should have this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
</dependencies>
<build>
<finalName>WebServices1</finalName>
</build>
</project>
这是我的控制器:
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
这是我的问候pojo课程:
package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
当我部署我的应用程序并以http://localhost:8081/WebServices1/rest/greeting
访问该网址时,我在浏览器中收到 HTTP 406 错误消息。
更新
根据我的mozilla firebug插件,这是我的请求标题:
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate
Accept-Language
en-US,en;q=0.5
Cache-Control
max-age=0
Connection
keep-alive
Cookie
JSESSIONID=9B49E7A23D6CDE85CAC4152E22834D35; textwrapon=false; textautoformat=false; wysiwyg=textarea
; m=34e2:|2663:t|c01:t|5cf4:t|4a01:t|54e1:t
Host
localhost:8081
User-Agent
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0
响应标题:
Content-Language
en
Content-Length
1067
Content-Type
text/html;charset=utf-8
Date
Mon, 22 Jun 2015 11:00:08 GMT
Server
Apache-Coyote/1.1
答案 0 :(得分:4)
问题在于你在pom.xml文件中的依赖关系。
在 Spring 4.1。* 版本中,Jackson库的pom.xml依赖项应包含以下内容:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
您可以在这些帖子中获得更多详细信息:
Spring JSON request getting 406 (not Acceptable) - bekur给出的答案
也在这里:
406 Spring MVC Json, not acceptable according to the request "accept" headers
另外,如果您将弹簧版本更改回4.0。*或其他任何内容,您的代码将有效。