Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.