Reputation: 1156
for @RequestParam
there is the option to declare the variable as Optional<T>
to find out if the parameter was set at all. Is there something similar for @RequestBody
members?
For example take the following class used as a @RequestBody
parameter in a patch endpoint. Is it possible to differentiate between a null name and no name at all? I'd like to only set the name to null, if the client explicitly sets the name to null (rather than leaving out the name).
public class UserPatchRequestBody {
public String name
}
Upvotes: 3
Views: 1313
Reputation: 195
You could do something like
@RequestParam(defaultValue = "empty", required = false) String name
So, if the client didn't place anything you would have "empty" (or whatever you want) instead of "null".
Upvotes: 1