【问题标题】:RESTful json response failing on tomcat but works in GlassFishRESTful json 响应在 tomcat 上失败,但在 GlassFish 中有效
【发布时间】:2012-05-12 03:16:37
【问题描述】:

这是我使用 JSON 响应返回的方法。此代码适用于 GlassFish,但在 tomcat 7.0.27 上运行时出现错误

HTTP 状态:406

此请求标识的资源只能生成具有根据请求“接受”标头()不可接受的特征的响应。

@RequestMapping(value = "/test", headers="Accept=application/json", method = RequestMethod.POST)
public @ResponseBody
Map<String, ? extends Object> getAuthPOST(@RequestBody String jsonBody) throws JSONException {                        
    JSONObject j = new JSONObject(jsonBody);
    System.out.println(j.keys());
    Iterator i = j.keys();
    Map<String, Object> result = new HashMap<String, Object>();
    while (i.hasNext()) {            
        String key = (String) i.next();
        result.put(String.valueOf(key), String.valueOf(j.getString(key)));
    }        
    return result;

}

更新:

这是我收到请求时的标头

HTTP/1.1 406 不可接受 内容长度:1070 服务器:Apache-Coyote/1.1 内容类型:text/html;charset=utf-8 日期:格林威治标准时间 2012 年 5 月 2 日星期三 15:47:24 连接:关闭

它将 Content-Type 更改为 text/html。

这是我的请求标头:

POST /SpringMVC/login/test HTTP/1.1 接受:应用程序/json 内容类型:application/json

【问题讨论】:

    标签: json spring jakarta-ee tomcat spring-mvc


    【解决方案1】:

    您没有提到您使用的是哪个版本的 spring-mvc,但如果您使用的是 3.1,您可以将“produces”参数添加到您的 @RequestMapping。 produces="application/json"

    【讨论】:

    • 是的,我使用的是 3.1。生产没有工作。我仍然收到与响应相同的错误。
    • 我通过包含 jackson-jaxrs 库来修复它。
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2012-05-24
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多