Reputation: 2580
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
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
Reputation: 479
if(entity!=null){
return new ResponseEntity<T>(entity, HttpStatus.BAD_REQUEST);
}
T is data type
Upvotes: 1
Reputation: 1420
You can try this.
if (entity != null)
return ResponseEntity.badRequest().body(entity);
Upvotes: 1