且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JAX-RS进行Bean验证(轻松实现):无法识别参数名称

更新时间:2022-04-07 02:27:57

您可以尝试使用-parameters编译应用,或指示IDE进行编译,例如的情况下 eclipse:首选项-> java->编译器->存储有关方法参数的信息(可通过反射使用)"

You could try to compile your app with -parameters or instruct your IDE to do so, e.g. in case of eclipse: preferences -> java -> compiler -> "store information about method parameters (usable via reflection)"

将其设置为适当的位置之后,您需要指示Bean验证基础架构(例如)的hibernate-validator进行以下操作: 通过META-INF/validation.xml使用ReflectiveParameterNamer.

With that in place you then need to instruct the Bean Validation infrastructure (e.g. ) hibernate-validator to use the ReflectiveParameterNamer via META-INF/validation.xml.

<parameter-name-provider>org.hibernate.validator.parameternameprovider.ReflectionParameterNameProvider</parameter-name-provider>

另请参见休眠验证器配置

我可以可靠地使用 Paranamer库

META-INF/validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<validation-config
   xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
                    http://jboss.org/xml/ns/javax/validation/configuration
                    validation-configuration-1.1.xsd"
   version="1.1">
   <default-provider>org.hibernate.validator.HibernateValidator
   </default-provider>
   <message-interpolator>org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator
   </message-interpolator>
   <traversable-resolver>org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver
   </traversable-resolver>
   <constraint-validator-factory>org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl
   </constraint-validator-factory>
   <parameter-name-provider>org.hibernate.validator.parameternameprovider.ParanamerParameterNameProvider</parameter-name-provider>
</validation-config>

要使paranamer与wildfly一起使用,我需要创建一个parameter-namer jboss-module 并从hibernate-validator模块的module.xml引用该模块.

To get paranamer working with wildfly I needed to create a parameter-namer jboss-module and reference that module from the module.xml of the hibernate-validator module.

有了这个,我可以简单地写:

With that in place I could simply write:

@POST
public Response login(@NotNull @Valid @Named("authRequest") AuthRequest authRequest) {
    return Response.ok().build();
}
...

public class AuthRequest {

    @NotNull(message = AuthMessages.EMAIL_REQUIRED)
    public String email;

    @NotNull(message = AuthMessages.PASSWORD_REQUIRED)
    public String password;
}

对于通过curl发送的请求产生以下响应:

which yields the following response for a request sent via curl:

curl -H "Content-Type: application/json" -H "Accept: application/json" -d '{"email":"foo@bar.com"}' -v http://localhost:8080/javaweb-training/resources/auth

响应:

{"exception":null,"fieldViolations":[],"propertyViolations":[],"classViolations":[],"parameterViolations":[{"constraintType":"PARAMETER","path":"login.authRequest.password","message":"password.required","value":""}],"returnValueViolations":[]}%

...请注意login.authRequest.password而不是login.arg0.password

... note login.authRequest.password instead of just login.arg0.password