1、@Controller 是spring mvc controler的注解。
2、@ResponseBody 是将spring mvc controler的方法的返回值【对象、基本数据类型】放到响应体中的注解。
3、@RestController=@Controller+@ResponseBody
例1:@Controller+@ResponseBody:
@Controller
public class ResonseTestController {
@GetMapping("/response/{id}")
@ResponseBody
public User testPathVariableMultiParam(@PathVariable("id") Integer id){
User user=new User();
user.setId(id);
user.setAge(15);
user.setName("张三");
return user;
}
@GetMapping("/response/testGetAllUser")
@ResponseBody
public List<User> testGetAllUser(){
List<User> list=new ArrayList<User>();
User user1=new User();
user1.setId(1);
user1.setAge(15);
user1.setName("张三");
list.add(user1);
User user2=new User();
user2.setId(2);
user2.setAge(16);
user2.setName("李四");
list.add(user2);
User user3=new User();
user3.setId(3);
user3.setAge(17);
user3.setName("王五");
list.add(user3);
return list;
}
}
例2:@RestController:
@RestController
public class ResonseTestController {
@GetMapping("/response/{id}")
public User testPathVariableMultiParam(@PathVariable("id") Integer id){
User user=new User();
user.setId(id);
user.setAge(15);
user.setName("张三");
return user;
}
@GetMapping("/response/testGetAllUser")
public List<User> testGetAllUser(){
List<User> list=new ArrayList<User>();
User user1=new User();
user1.setId(1);
user1.setAge(15);
user1.setName("张三");
list.add(user1);
User user2=new User();
user2.setId(2);
user2.setAge(16);
user2.setName("李四");
list.add(user2);
User user3=new User();
user3.setId(3);
user3.setAge(17);
user3.setName("王五");
list.add(user3);
return list;
}
}