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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-09-11 11:00:00 +0000
Seen: 8 times
Last updated: Apr 11