Spring MVC Rest / JSON服务 [英] Spring MVC Rest / JSON service
本文介绍了Spring MVC Rest / JSON服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚尝试将它添加到我的示例控制器:
I just tried adding this to my sample controller:
@RequestMapping(value="/jsontest", method=RequestMethod.GET)
public @ResponseBody User getUserAsJson() {
User jsonUser = new User();
jsonUser.setFirstName("Mickey");
jsonUser.setLastName("Mouse");
jsonUser.setUsername("mmous");
return jsonUser;
}
但是,访问/ jsontest url结束于406 http错误=>不可以接受。
However, visiting the /jsontest url ends on a 406 http error => not acceptable.
那么...生成application / json响应,而不是返回jsp / html视图的当前工作方法是什么?
So... what's the currently working method for producing "application/json" responses, instead of returning jsp/html views?
我使用的是Spring Framework 3.0.6 RELEASE。
I'm using Spring Framework 3.0.6 RELEASE.
推荐答案
添加 Jackson 映射到您的CLASSPATH:
Add Jackson mapper to your CLASSPATH:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
请使用正确的接受标头呼叫您的网络服务,例如:
And call your web service with correct accept header, e.g.:
$ curl -H "Accept: application/json" localhost:8080/app/jsontest
或使用 $。getJSON )
。
这篇关于Spring MVC Rest / JSON服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文