Bean Validation 1.1介绍了
ParameterNameProvider
接口,用于在创建约束冲突对象时为方法和构造函数参数提供名称。
Hibernate Validator 5.2引入了
ReflectionParameterNameProvider
类,a
参数名称提供程序
使用反射获取实际参数名称的实现(要正常工作,需要使用
-parameters
编译器参数):
/**
* Uses Java 8 reflection to get the parameter names.
* <p>
* <p>For this provider to return the actual parameter names, classes must be compiled with the '-parameters' compiler
* argument. Otherwise, the JDK will return synthetic names in the form {@code arg0}, {@code arg1}, etc.</p>
* <p>
* <p>See also <a href="http://openjdk.java.net/jeps/118">JEP 118</a></p>
*
* @author Khalid Alqinyah
* @since 5.2
*/
public class ReflectionParameterNameProvider implements ParameterNameProvider {
@Override
public List<String> getParameterNames(Constructor<?> constructor) {
return getParameterNames(constructor.getParameters());
}
@Override
public List<String> getParameterNames(Method method) {
return getParameterNames(method.getParameters());
}
private List<String> getParameterNames(Parameter[] parameters) {
List<String> parameterNames = newArrayList();
for (Parameter parameter : parameters) {
// If '-parameters' is used at compile time, actual names will be returned. Otherwise, it will be arg0, arg1...
parameterNames.add(parameter.getName());
}
return parameterNames;
}
}
Dropwizard对其进行了扩展,并为JAX-RS添加了支持
@XxxParam
注释
JerseyParameterNameProvider
这也应该与其他JAX-RS实现一起工作:
/**
* Adds jersey support to parameter name discovery in hibernate validator.
* <p>
* <p>This provider will behave like the hibernate-provided {@link ReflectionParameterNameProvider} except when a
* method parameter is annotated with a jersey parameter annotation, like {@link QueryParam}. If a jersey parameter
* annotation is present the value of the annotation is used as the parameter name.</p>
*/
public class JerseyParameterNameProvider extends ReflectionParameterNameProvider {
@Override
public List<String> getParameterNames(Method method) {
Parameter[] parameters = method.getParameters();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
List<String> names = new ArrayList<>(parameterAnnotations.length);
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] annotations = parameterAnnotations[i];
String name = getParameterNameFromAnnotations(annotations).orElse(parameters[i].getName());
names.add(name);
}
return names;
}
/**
* Derives member's name and type from it's annotations
*/
public static Optional<String> getParameterNameFromAnnotations(Annotation[] memberAnnotations) {
for (Annotation a : memberAnnotations) {
if (a instanceof QueryParam) {
return Optional.of("query param " + ((QueryParam) a).value());
} else if (a instanceof PathParam) {
return Optional.of("path param " + ((PathParam) a).value());
} else if (a instanceof HeaderParam) {
return Optional.of("header " + ((HeaderParam) a).value());
} else if (a instanceof CookieParam) {
return Optional.of("cookie " + ((CookieParam) a).value());
} else if (a instanceof FormParam) {
return Optional.of("form field " + ((FormParam) a).value());
} else if (a instanceof Context) {
return Optional.of("context");
} else if (a instanceof MatrixParam) {
return Optional.of("matrix param " + ((MatrixParam) a).value());
}
}
return Optional.empty();
}
}
如果不使用Dropwizard,可以使用上面的代码创建自己的实现。
自定义
Validator
可以使用
ValidationConfig
类,并通过
ContextResolver<T>
机制:
public class ValidationConfigurationContextResolver
implements ContextResolver<ValidationConfig> {
@Override
public ValidationConfig getContext(final Class<?> type) {
ValidationConfig config = new ValidationConfig();
config.parameterNameProvider(new CustomParameterNameProvider());
return config;
}
}
然后注册
ValidationConfigurationContextResolver
在里面
ResourceConfig
.
请参阅
Jersey documentation about Bean Validation support
了解更多详情。