我想自动验证 Spring REST
服务上的 REST
参数。
我尝试使用@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?
最佳答案
这是请求参数验证的示例..
public ResponseEntity<AgencyResource> saveAgency(
@Valid @RequestBody AgencyResource agencyResource) {
return new ResponseEntity<AgencyResource>(agencyResource, HttpStatus.OK);
}
这来自帖子http://www.leveluplunch.com/java/tutorials/017-validate-spring-rest-webservice-request/
希望这有帮助。
谢谢, 保罗
关于java - 如何自动验证@RestController中的剩余参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28831503/