我使用Spring工具为eclipse编写了一个spring MVC应用程序,并将其部署到AWS Elastic Beanstalk上的tomcat 7环境中。但是当我尝试返回JSON时,我收到以下错误。
406- The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
但是当我在glassfish服务器上本地运行相同的应用程序时,它运行完美并返回预期的json,如下所示:
{"id":1,"message":"Hello, World!","description":"Hello, World!"}
这是我的代码:
控制器代码:
private static final String template = "Hello, %s!";
private final AtomicInteger counter = new AtomicInteger();
@RequestMapping(value = "/greeting",headers="Accept=*/*",method = RequestMethod.GET)
public @ResponseBody ResponseBean greeting(@RequestParam(value="name", required=false, defaultValue="World") String name,HttpServletResponse response , ModelMap model){
response.setContentType("application/json");
return new ResponseBean(counter.incrementAndGet(),String.format(template, name),String.format(template, name));
}
我的响应Bean代码:
package com.ramesh.beans;
public class ResponseBean {
private final int id;
private final String message;
private final String description;
public ResponseBean(int id,String message,String desc){
this.id = id;
this.message = message;
this.description = desc;
}
public int getId() {
return id;
}
public String getMessage() {
return message;
}
public String getDescription() {
return description;
}
}
我的servlet-context.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.ramesh.ws" />
</beans:beans>
有人可以帮帮我吗?如果需要,我愿意提供有关我的计划的更多信息。
我在chrome上的请求标题如下:
他们如下:
1 requests ❘ 1.3 KB transferred ❘ 250 ms (load: 256 ms, DOMContentLoaded: 257 ms)
HeadersPreviewResponseTiming
Remote Address:54.206.99.113:80
Request URL:http://ramesh-dev-gvssfipguk.elasticbeanstalk.com/greeting
Request Method:GET
Status Code:406 Not Acceptable
请求标题
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6,ms;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Host:ramesh-dev-gvssfipguk.elasticbeanstalk.com
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
响应标头
Connection:keep-alive
Content-Length:1070
Content-Type:text/html;charset=utf-8
Date:Tue, 20 May 2014 10:14:45 GMT
Server:Apache-Coyote/1.1
X-Cache:MISS from tx33vspep22a
答案 0 :(得分:0)
我最后通过更新我的控制器方法修复了它,如下所示:
@RequestMapping(value = "/greeting",produces="application/json",method = RequestMethod.GET)
public @ResponseBody ResponseBean greeting(@RequestParam(value="name", required=false, defaultValue="World") String name,HttpServletResponse response , ModelMap model){
response.setContentType("application/json");
return new ResponseBean(counter.incrementAndGet(),String.format(template, name),String.format(template, name));
}
似乎在部署到tomcat时我需要产生=&#34; application / json&#34;属性。