@ResponseBody修饰String和对象,前台接收的格式
程序员文章站
2022-07-12 19:41:48
...
1:String +ResponseBody = 返回值为String
@RequestMapping("/testResponseBody")
@ResponseBody
public String testResponseBody(){
return "success";
}
alert---success
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"}
3:String +ResponseBody = String
@RequestMapping("/testResponseBody3")
@ResponseBody
public String testResponseBody3(){
return JSON.toJSONString(SysResult.resultOK());
}
alert {"resultcode":0,"resultdesc":""}
字符串可以转换为json对象
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":""}下一篇: 细解spring mvc架构