IsaacLevon
IsaacLevon

Reputation: 2580

Spring Boot: How to return a BAD_REQUEST with an entity?

In my @RestController I'd like to return a BAD_REQUEST if the entity already exists.

I did this:

if (entity != null)
  return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(entity);

The problem is that when I put it to test, I see an empty body.

Why?

Upvotes: 0

Views: 3864

Answers (3)

Himashi Rodrigo
Himashi Rodrigo

Reputation: 61

Response Entity provides two nested builder interfaces. Hence we can access the capabilities directly through the static methods of ResponseEntity Try this one:

if (entity != null)
    return ResponseEntity.badRequest().body(entity);

Upvotes: 1

Lakshman
Lakshman

Reputation: 479

if(entity!=null){
    return new ResponseEntity<T>(entity, HttpStatus.BAD_REQUEST);
}

T is data type

Upvotes: 1

fatih
fatih

Reputation: 1420

You can try this.

if (entity != null)
   return ResponseEntity.badRequest().body(entity);

Upvotes: 1

Related Questions