jdflores
jdflores

Reputation: 417

How to return HttpStatus 204 in Mono webflux

It's possible return a Mono<Object> with statusCode 204 (NO_CONTENT)?

I need that, for example postman show something like this: enter image description here

Upvotes: 3

Views: 11862

Answers (5)

L_G
L_G

Reputation: 934

I'd suggest to use ResponseEntity , like this:

@RequestMapping(value = "/elb", method = RequestMethod.GET)
public ResponseEntity elbHealthCheck() {
    return HealthStatus.HEALTHY.equals(healthService.getHealthStatus())
            ? ResponseEntity.ok().build()
            : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}

Off course in your case, you just need to replace that HttpStatus code. Cheers!

Upvotes: 0

nekperu15739
nekperu15739

Reputation: 3698

It's not compatible return a Mono<Object> with a http status 204. If you want a 204 you should return Mono<Void> like this sample using routerFunctions:

public Mono<ServerResponse> checkWithout(final ServerRequest request) {
    return noContent()
        .build();
}

Upvotes: 1

Jin Kwon
Jin Kwon

Reputation: 21978

I recommend using ServerResponse.

public Mono<ServerResponse> create(MyEntity entity) {
    return just(save(entity))
            .map(created(fromCurrentRequestUri()
                                 .pathSegment("{id}")
                                 .build()
                                 .expand(entity.getId())
                                 .toUri()).build());
}

Upvotes: 1

ashanshamika
ashanshamika

Reputation: 147

Use Mono<ResponseEntity<Object>> instead of Mono<Object>. Response entity can be used to provide the proper response codes

For this case

return Mono.just(ResponseEntity.status(HttpStatus.NO_CONTENT).build());

Upvotes: 4

gears
gears

Reputation: 743

If using a Controller, this should do (replace path, method, etc as needed):

   @GetMapping(value = "/no-content")
   @ResponseStatus(HttpStatus.NO_CONTENT)
   @ResponseBody
   public Mono<Object> noContentMethod()
   {
      // ... processing here ...
      return Mono.empty();
   }

Upvotes: 4

Related Questions