我构建了一个Spring REST服务,并且我有一堆通过POST方法接收请求负载的端点。我在我的项目中包含了JSR 303规范,并且它对验证工作正常。现在我如何让我的应用程序发送一个JSON响应以及一个不同的状态码。目前该应用程序给出了一个400的错误页面。当没有满足javax.validation.constraints。*时,发送休息响应而不是HTML页面
更新:
我想通了,我需要包括BindingResult在我的方法,所以我可以从那里提取的错误。
@PostMapping(value = "/validateBankInformation", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
protected ResponseEntity<BusinessSolutionVO> validateBankInformation(@Valid @RequestBody BankInformation bankInformation, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
List<ObjectError> errors = bindingResult.getAllErrors();
for (ObjectError error : errors) {
System.out.println(error.getDefaultMessage());
}
}
}
到目前为止你做了什么? – delephin