【发布时间】:2017-02-12 23:55:42
【问题描述】:
我在@RestController 中添加了一个验证(@javax.validation.Valid),并在@RequestBody 类中使用了@NotEmpty 注释:
public class Invoice {
@NotEmpty
private String commentText;
// omitted
}
当调用缺少 commentText 的 REST 调用时,会向客户端返回正确的错误:
{
"timestamp": 1475599494823,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [ {
"codes": [
"NotEmpty.invoice.commentText",
"NotEmpty.commentText",
"NotEmpty.java.lang.String",
"NotEmpty"
],
"arguments": [ {
"codes": [
"invoice.commentText",
"commentText"
],
"defaultMessage": "commentText",
"code": "commentText"
}],
"defaultMessage": "may not be empty",
"objectName": "invoice",
"field": "commentText",
"bindingFailure": false,
"code": "NotEmpty"
}],
"message": "Validation failed for object='invoice'. Error count: 1",
"path": "/api/invoices/1"
}
然后我创建了具有以下内容的 src/main/resources/messages.properties:
NotEmpty.invoice.commentText=Please type a comment
问题是返回给客户端的是原始错误消息,而不是自定义的“请输入评论”。
我正在使用带有 YAML 配置的 SpringBoot 1.4 并在 Eclipse 中运行应用程序。
知道问题出在哪里吗?
【问题讨论】:
-
有关于这个问题的更新吗?
标签: java spring spring-boot bean-validation