我想在彈簧REST
服務上自動驗證REST
參數。如何自動驗證@RestController中的其餘參數?
我試過它使用@Valid @NotNull
,但其餘的請求不會自動被拒絕,但dao方法是用null參數執行的。爲什麼?
@RestController
public class RestController {
@RequestMapping(value = "/")
public Boolean getResponse(@Valid @NotNull @Length(max = 20) String username) {
return daoService.lookup(username); //is executed if username = null
}
}
我怎樣才能自動得到一個返回的HTTP錯誤,例如400?
我認爲你只是缺少@RequestBody,雖然不知道春天可以驗證輸入字符串。 – mavarazy 2015-03-03 12:37:17