我想創建一些基於Spring的REST服務示例。我想獲得基於Foo對象的JSON,但是當我嘗試使用curl發送請求時,它顯示406錯誤:「由此請求標識的資源只能根據請求生成不可接受的響應特徵」accept「頭文件「。這裏是我的代碼: RestControl.javaJava Spring JSON響應問題
package hello;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/app")
public class RestControl {
@RequestMapping(value="/get",produces="application/json", method=RequestMethod.GET)
@ResponseBody
public Foo getFoo(){
Foo f = new Foo();
f.setId(new Long(1));
f.setName("lol");
return f;
}
}
Foo.java:
package hello;
public class Foo {
private Long id;
private String name;
public void setId(Long id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
的web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<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>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
MVC-調度-servlet.xml中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<bean id="RestControl"
class="hello.RestControl" />
<bean id="foo"
class="hello.Foo" />
</beans>
Upd:
UPD2: org.codehaus.jackson 傑克遜映射器 - 翔升 1.9.13
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.12</version>
</dependency>
<!-- Spring ORM support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-framework.version}</version>
</dependency>
這是一個Maven項目嗎?你可以發佈你的'pom.xml'或列出你的依賴關係嗎?你有沒有合適的傑克遜圖書館? – woemler 2015-03-02 21:12:08
我附加了依賴項列表。看起來麻煩在那裏。我會很高興,如果你告訴我哪個依賴包括 – IcedNecro 2015-03-02 21:32:36
其實我認爲,你有所有的映射到位,你有捲曲命令的問題。無論如何,祝你好運...希望我的回答也幫助你。 – minion 2015-03-03 00:57:55