求助大佬!这个 @AliasFor 为什么不生效??

2019-03-23 00:06:50 +08:00
 rizon
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@Pattern(regexp = "^[\\u4e00-\\u9fa5\\w]+$")
@Constraint(validatedBy = { })
public @interface ChsNamePattern {

	@AliasFor(value = "message", annotation = Pattern.class)
    String message() default "自定义 message";

    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
}

想用这个自定义注解来简化的使用 @Pattern

但是 @validated 校验的时候 message 不会生效。而是会走默认的,大晚上的折腾好久了,好烦。。 是因为 @AliasFor 是 spring 的,校验工具不会去解析??

3039 次点击
所在节点    程序员
7 条回复
rizon
2019-03-23 00:48:25 +08:00
这个问题好难受啊。
我不想每个地方都要写这个 regex 啊。
feiyuanqiu
2019-03-23 02:12:52 +08:00
你想要实现什么效果?校验失败时抛出 @ChsNamePattern 的“自定义 message ”而不是 @Pattern 的 message ?实现这个有两个方法:

1. 使用 @ReportAsSingleViolation 注解

@ReportAsSingleViolation
@Pattern(regexp = "^[\\u4e00-\\u9fa5\\w]+$")
@Constraint(validatedBy = {})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface ChsNamePattern {}


2. 使用 @OverridesAttribute 注解

@Pattern(regexp = "^[\\u4e00-\\u9fa5\\w]+$")
@Constraint(validatedBy = {})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface ChsNamePattern {

@OverridesAttribute(constraint = Pattern.class, name = "message")
String message() default "自定义 message";
}


https://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintcomposition
feiyuanqiu
2019-03-23 02:20:24 +08:00
wuzhi1234
2019-03-23 08:28:08 +08:00
还特地为一个正则写个注解,这开发回报比也太低了,还不如直接维护个正则常量类或者枚举
rizon
2019-03-25 11:51:44 +08:00
@feiyuanqiu #2 感谢大佬!!!居然还有这种用法,这些冷门知识真的是不好找啊。。

关于问题写的不清楚非常抱歉。
我平常提问的时候,其实都有好好去想和写问题,让问题更加直接和简洁有效。就像你说的,描述目标。

当时提问的时候,是周六好像一两点的时候了,有事着急回家,然后又被这么个问题卡壳了好久,又着急又找不大答案。所以郁闷加急迫之下,就匆匆写了问题。 唉~
rizon
2019-03-25 13:26:56 +08:00
@feiyuanqiu #2 大佬阿,还有个问题麻烦问一下。

就是 ValidationMessages.properties 校验模版中文乱码。

我配置了 ValidationMessages.properties,去配置自定义的错误信息模板。使用的是 ut8 格式,但是会乱码,我尝试去按网上说的配置 messagesource 的 bean 对象也没有解决。现在只能把文件内容换成 native 格式的。
feiyuanqiu
2019-03-25 19:00:47 +08:00
@rizon #6 不能直接写中文,因为 .properties 文件的编码方式是 ISO-8859-1,中文字符需要转换为对应的 Unicode escape 编码。否则读取时就乱码了。可以使用 commons-text 库的一个方法做转换:

String s = "只能为 false";
String escaped = org.apache.commons.text.StringEscapeUtils.escapeJava(s);
System.out.println(escaped);

默认的中文消息模版在 org/hibernate/validator/ValidationMessages_zh_CN.properties,可以参考一下它。

"The encoding of a .properties file is ISO-8859-1, also known as Latin-1. All non-Latin-1 characters must be entered by using Unicode escape characters, e.g. \uHHHH where HHHH is a hexadecimal index of the character in the Unicode character set."

https://en.wikipedia.org/wiki/.properties#Format

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/547614

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX