Superwayne
Superwayne

Reputation: 1156

Spring Boot - Check if request body member is null or missing

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

Answers (1)

duppydodah
duppydodah

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

Related Questions