2015-10-06 33 views
4

我有一个通过POST请求接收JSON的端点。具有可选属性的请求主体

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 

} 

反正我有可以指定idDevice为可选?

如果我不在json内部发送idDevice,Spring会返回400错误。

回答

8

似乎将RequestBody设置为可选,使得任何属性都是可选的,不仅是完整的bean。

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