ResponseEntity:
code:
/*常用*/
@GetMapping("/hello")
public ResponseEntity<String> hello(){
return new ResponseEntity<>("hello word!",HttpStatus.OK);
}
/*设置HTTP头*/
@GetMapping("/hello")
public ResponseEntity<String> hello(){
HttpHeaders headers = new HttpHeaders();
headers.add("content-type","json");
return new ResponseEntity<>("hello word!",headers,HttpStatus.OK);
}
/*设置自定义标头*/
@GetMapping("/hello")
public ResponseEntity<String> hello(){
return ResponseEntity.ok()
.header("content-type","json")
.body("hello world");
}
/*返回对象的Json列表*/
@GetMapping("/hello")
public ResponseEntity<List<Data>> getData(){
return new ResponseEntity<>(Data.findAll(),HttpStatus.FOUND);
}
@ResponseBody:
code:
@ResponseBody
public User show(){
return new User("customer");
}
@ResponseStatus:
code:
/*专注于业务*/
@ResponseStatus(HttpStatus.FOUND)
public User postUser(){
return new User("Customer");
}