Ask Your Question
2

How do the return types of a rest controller differ between ResponseEntit<Mono>; and Mono<ResponseEntity>?

asked 2021-09-11 11:00:00 +0000

woof gravatar image

updated 2023-04-11 11:52:26 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-13 13:00:00 +0000

nofretete gravatar image

The return types of a rest controller differ between ResponseEntity<mono> and Mono<responseentity> in the following ways:

  1. ResponseEntity<mono>: This return type wraps the Mono result in a ResponseEntity. This can be useful when you want to customize the response headers or status code. For example:
@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);
}
  1. Mono<responseentity>: This return type returns a Mono that emits the ResponseEntity. This can be useful when you want to apply operations to the ResponseEntity, such as filtering or mapping. For example:
@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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-09-11 11:00:00 +0000

Seen: 15 times

Last updated: Apr 11 '23