Java SpringBoot RESTful bean验证的自定义消息不起作用

Java SpringBoot RESTful bean验证的自定义消息不起作用,java,spring,spring-boot,bean-validation,Java,Spring,Spring Boot,Bean Validation,我将验证(@javax.validation.Valid)添加到我的@RestController中,并在我使用的@NotEmpty注释的@RequestBody类中: public class Invoice { @NotEmpty private String commentText; // omitted } 调用缺少commentText的REST调用时,将向客户端返回正确的错误: { "timestamp": 1475599494823, "s

我将验证(
@javax.validation.Valid
)添加到我的
@RestController
中,并在我使用的
@NotEmpty
注释的
@RequestBody
类中:

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中运行该应用程序


知道问题可能在哪里吗?

不要使用
@NotEmpty
注释,尝试使用
javax的替代方法。验证
包:
@NotNull
@Size(min=1)
。 此外,您可能会发现此示例项目对您很有用:

如果出于某种原因需要使用@NotEmpty注释,那么应该创建一个文件ValidationMessages.properties并将其放入类路径中

为了避免密钥转换出现问题,您可以这样指示消息密钥:
@NotEmpty(message=“{NotEmpty.invoice.commentText}”)
。或者看看这里:

我尝试了
@javax.validation.constraints.NotNull
NotNull.unidentifiedInvoice.commentText=请在
消息.properties
中键入注释
,但它仍然返回默认的
“defaultMessage”:“可能不为null”
是否已将类名更改为unidentifiedInvoice?如果不尝试使用此密钥:
NotNull.invoice.commentText
。抱歉,是的,我使用了…但这不是问题的根本原因。此问题有任何更新吗?