响应
# 响应
在SpringMVC中,我们该如何返回物件给请求源呢?
# 页面返回
默认情况下仅支持返回.jsp
格式的
@Controller
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println(request.getHeader("Cache-Control"));
return "test.jsp";
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 文本返回
@Controller
@ResponseBody
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println(request.getHeader("Cache-Control"));
return "test.jsp";
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
和上面区别就在于@ResponseBody
# JSON对象返回
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public User save(){
User user = new User();
user.setName("林家君");
user.setAge(18);
return user;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# @ResponseBody
![[Pasted image 20220615164709.png]]