欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

@ResponseBody修饰String和对象,前台接收的格式

程序员文章站 2022-07-12 19:41:48
...

1:String +ResponseBody = 返回值为String

@RequestMapping("/testResponseBody") @ResponseBody public String testResponseBody(){ return "success"; }
alert---success
@ResponseBody修饰String和对象,前台接收的格式

2:对象+ResponseBody = json对象

@RequestMapping("/testResponseBody2") @ResponseBody public Student testResponseBody2(){ Student s = new Student(); s.setAge(11); s.setName("11"); return s; }

alert ---{"age":11,"name":"11"}

@ResponseBody修饰String和对象,前台接收的格式

3:String +ResponseBody = String

@RequestMapping("/testResponseBody3") @ResponseBody public String testResponseBody3(){ return JSON.toJSONString(SysResult.resultOK()); }

alert {"resultcode":0,"resultdesc":""}

@ResponseBody修饰String和对象,前台接收的格式
字符串可以转换为json对象
@ResponseBody修饰String和对象,前台接收的格式

4:void+Response

@RequestMapping("/testResponseBody5") public void testResponseBody5(HttpServletResponse response){ response.setContentType("text/html"); PrintWriter out= null; try { out = response.getWriter(); out.println(JSON.toJSONString(SysResult.resultOK())); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } {"resultcode":0,"resultdesc":""}
@ResponseBody修饰String和对象,前台接收的格式