使用JAX-RS进行Bean验证(轻松实现):无法识别参数名称 [英] Bean Validation with JAX-RS (rest-easy): parameter name not recognized

查看:103
本文介绍了使用JAX-RS进行Bean验证(轻松实现):无法识别参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有Bean验证功能的JAX-RS资源,并且按预期完成了这两项工作之间的集成.

I'm using JAX-RS resources with Bean Validation and integration between these two works as expected.

但是,在验证错误的情况下生成的默认错误消息将参数名称命名为arg0,就像这样

However, the default error messages generated in case of a validation error report parameter names as arg0, like so

[PARAMETER]
[login.arg0.password]
[password is required]
[]

对应的方法定义:

@POST //and other JAX-RS annotations
public Response login(
        @NotNull
        @Valid
        LoginBody loginBody) {

   [...]

protected static class LoginBody {

    @NotNull(message =  EMAIL_REQUIRED)
    public String email;

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

虽然我通常对这种消息模式很满意,但实际上要取消的却是无法识别原始参数名称,即i. e.我想看

While I'm generally fine with this message pattern, what actually is annyoing, is the fact that the original parameter name is not recognized, i. e. I'd rather like to see

login. loginBody .password代替 arg0 .

login.loginBody.password instead of arg0.

是否有解决此问题的简便方法,例如G.以某种方式为该参数提供一个明确的名称?

Is there an easy way to fix this, e. g. somehow provide an explicit name for that parameter?

我正在使用WildFly Swarm 2017.6.0.从我发现的结果来看,这意味着我拥有resteasy + resteasy-validator + hibernate-validator

I'm using WildFly Swarm 2017.6.0. From what I found out this means I have resteasy + resteasy-validator + hibernate-validator

谢谢.

推荐答案

您可以尝试使用-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

这篇关于使用JAX-RS进行Bean验证(轻松实现):无法识别参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆