【发布时间】:2017-01-07 12:38:37
【问题描述】:
我收到 HTTP 错误代码 406,错误描述如下:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
我的目标是在 json 响应中获取我的 POJO 类。请找到我的以下配置:
@RequestMapping(value="/testjson",produces="application/json")
public @ResponseBody Employee testjson () {
System.err.println("testing json");
Employee testEmp = new Employee("1", "Ankit", "Agarwal");
return testEmp;
}
spring-servlet.xml
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="rss" value="application/rss+xml" />
</map>
</property>
</bean>
【问题讨论】:
-
我在 spring-servlet.xml 中添加了
,我可以看到带有 POJO 类的 json 响应。 -
我有一个疑问。我已经删除了 spring-servlet.xml 中的 bean ContentNegotiatingViewResolver 并且还删除了 RequestMapping 中的produces="application/json"。我仍然可以在这个 url 上看到 json 响应。有谁知道它从哪里捕捉到的响应是 json 类型的?
标签: spring rest spring-mvc spring-rest