1 | initial version |
The return types of a rest controller differ between ResponseEntity<mono> and Mono<responseentity> in the following ways:
@GetMapping("/users/{id}")
public ResponseEntity<Mono<User>> getUserById(@PathVariable("id") String id) {
Mono<User> userMono = userRepository.getUserById(id);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(userMono);
}
@GetMapping("/users/{id}")
public Mono<ResponseEntity<User>> getUserById(@PathVariable("id") String id) {
Mono<User> userMono = userRepository.getUserById(id);
return userMono.map(user -> ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(user));
}
In summary, the main difference between these two return types is the order of the types they wrap. ResponseEntity<mono> wraps the Mono result in a ResponseEntity, while Mono<responseentity> returns a Mono that emits the ResponseEntity.