2016-11-16 133 views
0

我有一個簡單的彈簧休息控制器,看起來像這樣。彈簧控制器枚舉請求驗證

@RestController 
public class MyController { 

@RequestMapping(path = "mapping", method = RequestMethod.POST, produces = {"application/json"}) 
    public MyResponse create(@RequestBody MyModel requestParam 
    ) throws InvalidApplicationSentException, AuthenticationFailedException { 
// method body 
} 

這是用作請求參數的MyModel類。

public class MyModel { 
    private RequestType requestType; 
    // a lot of other properties .. 
} 

現在,當我嘗試調用此端點傳遞一個無效值的RequestType我回來的exeption:

org.springframework.http.converter.HttpMessageNotReadableException 
Could not read document: Can not construct instance of com.mypackage.RequestType from String value 'UNDEFINED': value not one of declared Enum instance names: [IMPROTANT, NOT_IMPORTANT] 

有沒有一種方式,當通過不正確的值春天將設置枚舉空而不是拋出一個錯誤?

我用彈簧4,我寧願與配置註解和不XML文件

回答