小编dup*_*dup的帖子

Spring 3.0使用jackson消息转换器进行JSON响应

我将我的messageconverter配置为杰克逊的

class Foo{int x; int y}
Run Code Online (Sandbox Code Playgroud)

并在控制器中

@ResponseBody
public Foo method(){
   return new Foo(3,4)
}
Run Code Online (Sandbox Code Playgroud)

从那个我期望从服务器返回一个JSON字符串{x:'3',y:'4'},没有任何其他配置.但得到我的ajax请求的404错误响应

如果使用@ResponseBody注释该方法,则将返回类型写入响应HTTP正文.返回值将使用HttpMessageConverters转换为声明的方法参数类型.

我错了吗 ?或者我应该使用序列化程序将我的响应对象转换为Json字符串,然后将该字符串作为响应返回.(我可以正确地进行字符串响应)或者我应该进行其他配置吗?比如为Foo类添加注释

这是我的conf.xml

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
Run Code Online (Sandbox Code Playgroud)

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
  <list>
    <ref bean="jacksonMessageConverter"/>
  </list>
</property>
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc jackson

24
推荐指数
2
解决办法
7万
查看次数

标签 统计

jackson ×1

java ×1

json ×1

spring ×1

spring-mvc ×1