gpt4 book ai didi

json - Spring 启动 : ValidationMessages found but ContributorValidationMessages not found?

转载 作者:行者123 更新时间:2023-12-05 07:37:59 29 4
gpt4 key购买 nike

我正在使用 Spring Boot 构建 Web API。端点之一采用 JSON 发布请求。我想在某些字段上设置验证消息。所以我在/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 之外我还需要它?

编辑:

添加一个ContributorValidationMessages.properties在与ValidationMessages.properties同路径同行后,好像什么都找到了。

ValidationMessages found.
ContributorValidationMessages found.
org.hibernate.validator.ValidationMessages found.

但是,当我用空的 doorId 尝试相同的请求时,它只返回 406。验证消息没有在任何地方显示。

最佳答案

ContributorValidationMessages 是一个属性文件,用于放置自定义约束的默认消息。

根据相关blog post :

The bundle ContributorValidationMessages is queried by Hibernate Validator if the standard ValidationMessages bundle doesn’t contain a given message key, allowing library authors to provide default messages for their constraints as part of their JAR.

并根据相关document :

This mechanism is also helpful when creating large multi-module applications: Instead of putting all the constraint messages into one single bundle, you can have one resource bundle per module containing only those messages of that module.

关于json - Spring 启动 : ValidationMessages found but ContributorValidationMessages not found?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255380/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com