Spring的Json没有得到适当的回应 [英] Spring's Json not being resolved with appropriate response

查看:94
本文介绍了Spring的Json没有得到适当的回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Spring中的控制器返回一个JSON响应无法使用Jackson类作为推荐3.0 3.0。我当然在我的班级路径中有杰克逊jar文件(jackson-core-asl-1.5.5.jar& jackson-mapper-asl-1.5.5.jar)。

I've tried to have a controller in Spring return a JSON response to no avail using the Jackson classes as recommended with 3.0. I've got the jackson jar files(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar) in my class path of course.

对于appconfig.xml条目,我不确定是否需要这些。我把它们放在那里作为绝望的最后一幕,然后回到了'时尚非json ajax'。

As for the appconfig.xml entries, I'm not sure I need these. I've put them in there as a last act of desperation before returning to ol' fashion non-json ajax.

在调试中,我看到控制器获取请求,返回foo,然后在firebug中获得406.

In debug, I watch the controller get the request, return the foo and then, in firebug, get a 406.

错误消息如下:
设置为debug时从记录器中:
org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示

The error messages are as follows: From the logger when set to debug: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

来自响应:
(406)此请求标识的资源只能根据请求acceptheaders()生成具有不可接受特征的响应。

From the response: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

我的appconfig.xml在这里:

My appconfig.xml is here:

    <!-- Configures support for @Controllers -->
    <mvc:annotation-driven />

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
    <map>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
  </property>
  <property name="viewResolvers">
    <list>
      <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    </list>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </list>
  </property>
</bean>
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"></property>
    </bean>

我的控制器

@RequestMapping(value="foo/bar", method=RequestMethod.GET)
public @ResponseBody foo getFoo(@RequestParam String fooId) {
    return new foo(fooId); 
}

在jsp上,进行ajax调用:

On the jsp, where the ajax call is made:

function addRow() {
$.getJSON("foo/bar",{ fooId: 1} , function(data) {
alert("it worked.");        
});

}

让我知道是否有任何更多需要的信息。

Let me know if there's any more info that is needed.

推荐答案

摆脱所有杰克逊豆,以及谈判解析器中的json映射。 mvc:annotation-driven 应该配置Jackson序列化工作所需的一切。

Get rid of all Jackson beans, and of the json mapping in the negotiating resolver. the mvc:annotation-driven should configure everything you need for the Jackson serialization to work.

这篇关于Spring的Json没有得到适当的回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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