SprimgMVC 三 :返回json形式数据
controller内的方法有时候不仅仅返回视图名称,ModelAndView ,还有可能返回对象,此时就可以使用json形式返回
1.添加jackson-core-asl-xxx.jar和jackson-mapping-asl-xxx.jar
2.在controller中添加方法:
@RequestMapping(value="/{username}",method=RequestMethod.GET,params="json") @ResponseBody public User returnJson(@PathVariable String username){ User user = new User("tomcat","123",11); return user; }
因为是get方式,通过注解中的params指定url中?后面的参数名称
3.此时通过url访问此方法
http://localhost:8080/springmvcdemo/user/tomcat?json 就可以以json形式返回对象了
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30