英文:
HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Optional<java.lang.String>'
问题
你好,我正在尝试在YAML中进行验证:
- name: campo
in: query
schema:
type: string
pattern: '[S|N]' #pattern
maxLength: 1
使用Maven Spring Boot和委托。在RestController上,我添加了@Validated
注解,我可以验证required=true
的字段,但是如果字段不是必需的,根据架构设计,参数将是Optional<String>
。对于这个java.util.Optional
,尝试测试和验证会引发以下错误:
{
"timestamp": "2020-10-21T07:50:18.651+0000",
"status": 500,
"error": "Internal Server Error",
"message": "HV000030: 找不到约束'javax.validation.constraints.Size'的验证器,验证类型为'java.util.Optional<java.lang.String>'。请检查'metodo.campo'的配置",
"path": "/%7BbasePath%7D/microservicio/metodo"
}
是否有解决此问题的方法,以便正确处理Optional<String>
的验证?如果可以在YAML + 验证(@Valid和@Validated)中完成,我们不想在Java上编写所有验证代码。
编辑:@Size和其他验证是使用OpenAPI版本3.0.3从YAML生成的。
英文:
Hello guys I'm trying to get the validation on the YAML
- name: campo
in: query
schema:
type: string
pattern: '[S|N]' #pattern
maxLength: 1
Using maven spring boot and delegate. On the RestController I added annotation @Validated
and I'm able to validate required=true fields, but if fields are not require, as per architecture design, parameter will be Optional<String>
. For this java.util.Optional
, trying to test and validate, throw title error:
{
"timestamp": "2020-10-21T07:50:18.651+0000",
"status": 500,
"error": "Internal Server Error",
"message": "HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Optional<java.lang.String>'. Check configuration for 'metodo.campo'",
"path": "/%7BbasePath%7D/microservicio/metodo"
}
¿Anyway to solve this issue to have this Optional<String>
validation proccessed correctly? We don't want to write all code for validation on Java if can be done on YAML + validation (@Valid and @Validated).
EDIT: The validation @Size and others is generated from YAML with open api version 3.0.3
答案1
得分: 5
你应该写:Optional<@Size String>
(这将检查可选项内部的字符串大小),而不是@Size Optional<String>
(这将检查可选项的大小,这是没有意义的)。
参见Hibernate Validator:使用 java.util.Optional
:
package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;
public class Car {
private Optional<@MinTowingCapacity(1000) Integer> towingCapacity = Optional.empty();
//...
}
英文:
You should write: Optional<@Size String>
(which would check the size of the String inside the optional) instead of @Size Optional<String>
(which is supposed to check the size of the optional, which is meaningless).
See Hibernate Validator: With java.util.Optional
:
package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;
public class Car {
private Optional<@MinTowingCapacity(1000) Integer> towingCapacity = Optional.empty();
//...
}
答案2
得分: 0
较新版本的 Jakarta Bean Validation 2.0 自2019年8月起新增对 Optional
的验证支持。您可以在它们的变更日志摘要 2. 新内容 中找到这些信息:
> 对 java.util.Optional
的支持
截至今年9月,唯一获得认证的实现是 Hibernate Validator 6.0.17.Final
。
考虑进行升级,否则您将不得不省略大小验证并手动执行它。
英文:
The newer version Jakarta Bean Validation 2.0 newly supports Optional
validation as of August 2019. You can find this information out at their changelog summary 2. What’s new:
> Support for java.util.Optional
The only certified implementation, is Hibernate Validator 6.0.17.Final
as of September this year.
Consider an upgrade, ohterwise you would have to omit the size validation and perform it manually.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论