Spring-MVC 406 不可接受而不是 JSON 响应 [英] Spring-MVC 406 Not Acceptable instead of JSON Response

查看:37
本文介绍了Spring-MVC 406 不可接受而不是 JSON 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring 3.0.6 返回一个 JSON 响应,但我收到一个 406 响应不可接受",其描述为:此请求标识的资源只能生成具有特征的响应不接受根据请求接受"标头()."

I'm trying to return a JSON response with Spring 3.0.6, but I get a 406 response "Not Acceptable", with the description: "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()."

我知道一个非常相似的问题之前问过,但我不能让它为我的项目工作,尽管有很多测试,我不明白我做错了什么.

I know that a very similar question has been asked before, but I can't make it work for my project, despite many tests and I don't understand what I'm doing wrong.

在我的 Maven pom.xml 中,我有以下内容:

In my Maven pom.xml I've the following:

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.8.5</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-asl</artifactId>
  <version>1.8.5</version>
  <scope>compile</scope>
</dependency>

在 web.xml 中我引用了 webmvc-config.xml,并且日志确认已加载.

In web.xml I reference webmvc-config.xml, and the log confirms that is loaded.

<servlet>
    <servlet-name>mainServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

在 webmvc-config.xml 中,我有以下内容:

In webmvc-config.xml I've the following:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
    </bean> 
    <mvc:annotation-driven />

我的控制器是:

@Controller
public class ClassifiedController {

    @RequestMapping(value = "/classified/{idClassified}", headers = "Accept=*/*",
                    method = RequestMethod.GET)
    @ResponseBody
    public final Classified getClassified(@PathVariable final int idClassified) {
        ...

我尝试使用或不使用 headers 参数,结果相同.如果我调用 URL直接使用 Firefox,请求标头包含以下内容(使用 firebug 检查):

I tried with or without the headers parameter with the same results. If I call the URL directly with Firefox the Request Headers contain the following (checked with firebug):

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

如果我使用以下 JQuery:

If I use the following JQuery:

$.ajax({
        url: '/classified/38001',
        type: 'GET',
        dataType: 'json'
});

发送以下标头:

Accept: application/json, text/javascript, */*; q=0.01

在这两种情况下,结果都是 406 错误.我不知道我还应该检查什么它起作用了.

In both cases the result is a 406 error. I don't know what else should I check to make it work.

更新:我决定通过 Spring 进行调试,我发现 Jackson 被正确调用,并且在 org.codehaus.jackson.map.ser.StdSerializerProvider 中,方法 _findExplicitUntypedSerializer 包含以下代码:

UPDATE: I decided to debug through Spring and I found out that Jackson was called correctly and in org.codehaus.jackson.map.ser.StdSerializerProvider the method _findExplicitUntypedSerializer contains the following code:

try {
    return _createAndCacheUntypedSerializer(runtimeType, property);
} catch (Exception e) {
    return null;
}

这很不幸,因为隐藏了问题的根源.使用调试器,我发现该异常包含一个非常具有描述性的错误消息:

This is unfortunate because hides the source of the problem. With the debugger I found out that that exception contained a very descriptive error message:

Conflicting getter definitions for property "reminded": 
ClassifiedImpl#isReminded(0 params) vs
ClassifiedImpl#getReminded(0 params)

现在我看到错误消息是一个愚蠢的错误并且很容易修复,但没有它就不那么明显了.事实上,解决这个问题,导致了一个有效的序列化.

Now that I see the error message is a silly mistake and easy to fix, but without that it wasn't that obvious. In fact, fixing the problem, leaded to a working serialization.

推荐答案

关于MappingJacksonJson 处理,您需要确保 Jackson ObjectMapper 支持 您的对象类型用于序列化.

In terms of the MappingJacksonJson processing, you'll need to make sure that the Jackson ObjectMapper supports your object type for serialisation.

这篇关于Spring-MVC 406 不可接受而不是 JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆