【发布时间】:2018-06-23 15:14:06
【问题描述】:
我正在使用 Spring Boot 构建 Web API。端点之一接受带有 JSON 的 post 请求。我想在某些字段上设置验证消息。所以我在/src/main/resources 下添加了ValidationMessages.properties 和application.properties。在该文件中添加了以下几行:
doorid.notnull=Door ID is required
doorid.notblank=Door ID is empty
在我的 JSON 实体类中,我将字段“doorId”注释如下:
@NotNull(message = "{doorid.notnull}")
@NotBlank (message = "{doorid.notblank}")
private String doorId;
当我使用 doorId 作为空字符串测试 API 时,它只返回代码 406,没有任何消息。在服务器日志中,它说:
ValidationMessages found.
ContributorValidationMessages not found.
ContributorValidationMessages 到底是什么?除了ValidationMessages,为什么我还需要那个?
编辑:
在与ValidationMessages.properties同行的同一路径中添加ContributorValidationMessages.properties后,似乎找到了一切。
ValidationMessages found.
ContributorValidationMessages found.
org.hibernate.validator.ValidationMessages found.
但是,当我用空的doorId 尝试相同的请求时,它只返回 406。验证消息没有显示在任何地方。
【问题讨论】:
标签: json hibernate rest spring-boot bean-validation