【问题标题】:Tomcat on AWS ElasticBeanstalk doesn't Accept JSONAWS ElasticBeanstalk 上的 Tomcat 不接受 JSON
【发布时间】:2014-07-06 21:04:41
【问题描述】:

我使用 eclipse 的 spring 工具编写了一个 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

【问题讨论】:

  • A 406 表示客户端\浏览器发送了服务器无法提供的Accepts。您是否使用与远程测试相同的平均值(浏览器/等)进行本地测试?我不熟悉 AWS Elastic Beanstalk,但我大胆猜测 Tomcat 实例和 Web 之间可能存在某些东西,例如 Apache Web 服务器。有可能会被阻止。您可以在浏览器中打开调试工具并查看正在发送的 Accepts 标头吗? HTTP状态码参考:en.wikipedia.org/wiki/List_of_HTTP_status_codes
  • 感谢 CodeChimp 的快速回复。我使用 Google Chrome 并检查了请求标头。我已将它们添加到我的问题中。
  • 我相信这是tomcat服务器本身的问题。因为当我尝试在本地部署 tomcat 服务器时也会发生这种情况。
  • 您的请求/响应标头应该发送“应用程序/json”。你可以试试吗?

标签: json spring tomcat spring-mvc amazon-web-services


【解决方案1】:

我终于通过更新我的控制器方法来修复它,如下所示:

 @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 时我需要 products="application/json" 属性。

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 2013-06-18
    • 2017-10-29
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2017-08-02
    • 2015-01-29
    • 2014-10-20
    相关资源
    最近更新 更多