Have a restcontroller mapping with a @RequestBody TypeA in it.
TypeA is resolved correctly, even if I send a {} as a body.
The thing I'm trying to achieve is to have the same logic for when the body isn't sent at all (the @RequestBody is null).
Code:
@PostMapping('/post')
public ResponseEntity processPost(@RequestBody(required=false) Optional<TypeA> body) {
return service.someAction(body.orElse(new TypeA());
}
So far, the only possible solution I see is to set the required = false, wrap the type in Optional, and work with the .orElseGet(...new), which works, but doesn't look valid.
Is there a proper way to have this?
but doesn't look valid.- Personally, I think that the only thing wrong is treating{}andnullas the same. But if you really need this, I don't see anything wrong with your code