Spring ResponseEntity
- ResponseEntity处理http响应
1. ResponseEntity
- 标识了整个http响应:状态码、头部信息以及相应体内容
- 如果要使用ReponseEntity,必须在请求点返回,通常在spring rest中实现。
1.1. 内嵌接口
- HeadersBuilder:不能设置任何响应体属性
- BodyBuilder(是上面的子接口)
@GetMapping("/hello")
ResponseEntity<String> hello() {
return ResponseEntity.ok("Hello World!");
}
1.2. 常见相应响应
- BodyBuilder accepted();
- BodyBuilder badRequest();
- BodyBuilder created(java.net.URI location);
- HeadersBuilder<?> noContent();
- HeadersBuilder<?> notFound();
- BodyBuilder ok();
- BodyBuilder status(HttpStatus status)
- BodyBuilder status(int status)
1.2.1. status()的使用
@GetMapping("/age")
ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
if (isInFuture(yearOfBirth)) {
return ResponseEntity.badRequest()
.body("Year of birth cannot be in the future");
}
return ResponseEntity.status(HttpStatus.OK)
.body("Your age is " + calculateAge(yearOfBirth));
}
1.3. 简单使用
@GetMapping("/age")
ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
if (isInFuture(yearOfBirth)) {
return new ResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK);
}
1.4. 设置Http响应头
//方式一
@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("Custom-Header", "foo");
return new ResponseEntity<>("Custom header set", headers, HttpStatus.OK);
}
//方式二
@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
return ResponseEntity.ok()
.header("Custom-Header", "foo")
.body("Custom header set");
}
-
.body()
返回的是ResponseEntity,而并不是BodyBuilder,需要在最后进行调用。