下面列出了javax.ws.rs.QueryParam#value ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static QueryParam createQueryParam(Parameter parameter)
{
return new QueryParam()
{
@Override
public String value()
{
QueryParam annotation = parameter.getAnnotation(QueryParam.class);
if (annotation != null)
{
return annotation.value();
}
return parameter.getName();
}
@Override
public Class<? extends Annotation> annotationType()
{
return QueryParam.class;
}
};
}
private String _getParamName(Annotation[] annotations) {
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationClass = annotation.getClass();
if (CookieParam.class.isAssignableFrom(annotationClass)) {
CookieParam cookieParam = (CookieParam) annotation;
return cookieParam.value();
}
if (FormParam.class.isAssignableFrom(annotationClass)) {
FormParam formParam = (FormParam) annotation;
return formParam.value();
}
if (HeaderParam.class.isAssignableFrom(annotationClass)) {
HeaderParam headerParam = (HeaderParam) annotation;
return headerParam.value();
}
if (QueryParam.class.isAssignableFrom(annotationClass)) {
QueryParam queryParam = (QueryParam) annotation;
return queryParam.value();
}
}
return null;
}
@Override
public String getParameterName(QueryParam parameterAnnotation) {
return parameterAnnotation.value();
}
@AroundInvoke
public Object validateMethodInvocation(InvocationContext invocationContext) throws Exception {
Validator validator = validatorFactory.getValidator();
if (validator == null) {
return invocationContext.proceed();
}
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(invocationContext.getTarget());
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
Path propertyPath = constraintViolation.getPropertyPath();
Path.Node lastPathNode = null;
for (Path.Node pathNode : propertyPath) {
lastPathNode = pathNode;
}
if ((lastPathNode != null) && (lastPathNode.getKind() == ElementKind.PROPERTY)) {
Object leafBean = constraintViolation.getLeafBean();
Class<?> leafBeanClass = leafBean.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(leafBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String propertyDescriptorName = propertyDescriptor.getName();
if (propertyDescriptorName.equals("targetClass")) {
Method readMethod = propertyDescriptor.getReadMethod();
leafBeanClass = (Class<?>) readMethod.invoke(leafBean);
}
}
Field leafBeanField = leafBeanClass.getDeclaredField(lastPathNode.getName());
String paramName = null;
Annotation[] annotations = leafBeanField.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof CookieParam) {
CookieParam cookieParam = (CookieParam) annotation;
paramName = cookieParam.value();
break;
}
else if (annotation instanceof FormParam) {
FormParam formParam = (FormParam) annotation;
paramName = formParam.value();
break;
}
else if (annotation instanceof HeaderParam) {
HeaderParam headerParam = (HeaderParam) annotation;
paramName = headerParam.value();
break;
}
else if (annotation instanceof QueryParam) {
QueryParam queryParam = (QueryParam) annotation;
paramName = queryParam.value();
}
}
String interpolatedMessage = constraintViolation.getMessage();
if (messageInterpolator != null) {
interpolatedMessage = messageInterpolator.interpolate(constraintViolation.getMessageTemplate(),
new MessageInterpolatorContextImpl(constraintViolation), mvcContext.getLocale());
}
mutableBindingResult.addValidationError(new ValidationErrorImpl(paramName, interpolatedMessage,
constraintViolation));
}
}
return invocationContext.proceed();
}
public static Parameter getParameter(int index, Annotation[] anns, Class<?> type) {
Context ctx = AnnotationUtils.getAnnotation(anns, Context.class);
if (ctx != null) {
return new Parameter(ParameterType.CONTEXT, index, null);
}
boolean isEncoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
BeanParam bp = AnnotationUtils.getAnnotation(anns, BeanParam.class);
if (bp != null) {
return new Parameter(ParameterType.BEAN, index, null, isEncoded, null);
}
String dValue = AnnotationUtils.getDefaultParameterValue(anns);
PathParam a = AnnotationUtils.getAnnotation(anns, PathParam.class);
if (a != null) {
return new Parameter(ParameterType.PATH, index, a.value(), isEncoded, dValue);
}
QueryParam q = AnnotationUtils.getAnnotation(anns, QueryParam.class);
if (q != null) {
return new Parameter(ParameterType.QUERY, index, q.value(), isEncoded, dValue);
}
MatrixParam m = AnnotationUtils.getAnnotation(anns, MatrixParam.class);
if (m != null) {
return new Parameter(ParameterType.MATRIX, index, m.value(), isEncoded, dValue);
}
FormParam f = AnnotationUtils.getAnnotation(anns, FormParam.class);
if (f != null) {
return new Parameter(ParameterType.FORM, index, f.value(), isEncoded, dValue);
}
HeaderParam h = AnnotationUtils.getAnnotation(anns, HeaderParam.class);
if (h != null) {
return new Parameter(ParameterType.HEADER, index, h.value(), isEncoded, dValue);
}
CookieParam c = AnnotationUtils.getAnnotation(anns, CookieParam.class);
if (c != null) {
return new Parameter(ParameterType.COOKIE, index, c.value(), isEncoded, dValue);
}
return new Parameter(ParameterType.REQUEST_BODY, index, null);
}
private String _getParamName(Annotation[] annotations) {
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationClass = annotation.getClass();
if (CookieParam.class.isAssignableFrom(annotationClass)) {
CookieParam cookieParam = (CookieParam) annotation;
return cookieParam.value();
}
if (FormParam.class.isAssignableFrom(annotationClass)) {
FormParam formParam = (FormParam) annotation;
return formParam.value();
}
if (HeaderParam.class.isAssignableFrom(annotationClass)) {
HeaderParam headerParam = (HeaderParam) annotation;
return headerParam.value();
}
if (QueryParam.class.isAssignableFrom(annotationClass)) {
QueryParam queryParam = (QueryParam) annotation;
return queryParam.value();
}
}
return null;
}