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

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

问题描述

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

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参数并获得相同的结果。如果我直接使用Firefox调用URL
,请求标题包含以下内容(使用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.

UPDATE :我决定通过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天全站免登陆