Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request Body with optional property

I have an endpoint which receives a JSON through POST request.

RequestMapping(value = "/app/login", method = RequestMethod.POST,
        headers = { "Content-type=application/json" })
@ResponseBody
public LoginResponse logIn(@RequestBody LoginRequest jsonRequest) {
   // code
}

LoginRequest:

public class LoginRequest {

    private String user;

    private String password;

    private String idPush;

    private Integer idDevice;

    // getters and setters

}

Is there anyway I can specify idDevice as optional?

If I don't send idDevice inside the json, Spring returns a 400 error.

like image 603
niegus Avatar asked Oct 06 '15 13:10

niegus


Video Answer


1 Answers

It seems that setting the RequestBody to optional, makes any property optional, not only the full bean.

public LoginResponse logIn(@RequestBody(required=false) LoginRequest jsonRequest) {
like image 107
niegus Avatar answered Oct 10 '22 00:10

niegus