Request Body with optional property

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

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

}

Anyway, can I specify idDevice as optional?

If I do not send idDevice inside json, Spring returns a 400 error.

+4
source share
1 answer

Setting RequestBody to optional seems to make any property optional, not just a full bean.

public LoginResponse logIn(@RequestBody(required=false) LoginRequest jsonRequest) {
+9
source

Source: https://habr.com/ru/post/1610435/


All Articles