Spring 4:拦截时无法获取参数注解?
Spring 4: cannot get arguments annotations at interception?
我有这个方法:
@Override
public ClientDomain addClient(@Valid ClientDomain client) throws ValidationException {
Client clientEntity = this.transformer.transform(client);
Client clientEntityResponse = this.dao.save(clientEntity);
return this.transformer.revert(clientEntityResponse);
}
为了拦截它,我使用了 Spring 4 AOP。这就是我所拥有的:
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class ValidationInterceptor
implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Validating " + invocation.getMethod().getName() + " method");
Object[] asd = invocation.getArguments(); // Get object arguments
invocation.proceed();
return null;
}
}
所以,我想获取参数注释,在这种情况下,我希望收到@Valid 注释,但我找不到执行此操作的方法。我已经试过了
for (int i = 0; i < asd.length; i++) {
System.out.println("Param: " + asd[i].toString());
Annotation[][] annotations = invocation.getMethod().getParameterAnnotations();
for (Annotation annotation : annotations[i]) {
System.out.println("---" + annotation.getClass().getSimpleName());
}
}
但是我在 getParametersAnnotations() 上收到一个空注释对象
你知道我该怎么做吗?
我需要一次从 RequestMapping 注释中提取值。我使用过 HandlerInterceptorAdapter。如果 Valid
注释在花括号中不包含任何参数,我不确定你想通过获取它来实现什么,但也许你可以调整这段代码:
@Component
public class YourNameOfInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
String[] requestMappingValues = ((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class).value();
for (String value : requestMappingValues) {
//you can get values from for example @RequestMapping("/abc") here
}
}
return true;
}
}
我有这个方法:
@Override
public ClientDomain addClient(@Valid ClientDomain client) throws ValidationException {
Client clientEntity = this.transformer.transform(client);
Client clientEntityResponse = this.dao.save(clientEntity);
return this.transformer.revert(clientEntityResponse);
}
为了拦截它,我使用了 Spring 4 AOP。这就是我所拥有的:
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class ValidationInterceptor
implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Validating " + invocation.getMethod().getName() + " method");
Object[] asd = invocation.getArguments(); // Get object arguments
invocation.proceed();
return null;
}
}
所以,我想获取参数注释,在这种情况下,我希望收到@Valid 注释,但我找不到执行此操作的方法。我已经试过了
for (int i = 0; i < asd.length; i++) {
System.out.println("Param: " + asd[i].toString());
Annotation[][] annotations = invocation.getMethod().getParameterAnnotations();
for (Annotation annotation : annotations[i]) {
System.out.println("---" + annotation.getClass().getSimpleName());
}
}
但是我在 getParametersAnnotations() 上收到一个空注释对象
你知道我该怎么做吗?
我需要一次从 RequestMapping 注释中提取值。我使用过 HandlerInterceptorAdapter。如果 Valid
注释在花括号中不包含任何参数,我不确定你想通过获取它来实现什么,但也许你可以调整这段代码:
@Component
public class YourNameOfInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
String[] requestMappingValues = ((HandlerMethod) handler).getMethodAnnotation(RequestMapping.class).value();
for (String value : requestMappingValues) {
//you can get values from for example @RequestMapping("/abc") here
}
}
return true;
}
}