如何在方法拦截器中获取用@RequestBody注释的参数值?

时间:2015-08-24 06:23:43

标签: java spring spring-mvc

我有一个控制器,其参数之一有@RequestBody个注释。该参数还具有自定义注释@JsonData

public String updateUIMetadata(@PathVariable("caseId") final String caseId,
        @RequestBody @JsonData(schemaLocation = "schema/metadata_schema.json") final String metadataJson) {

          //...

}

我有一个拦截此方法的方法拦截器。我希望在拦截器中获得metadataJson的值。我尝试使用handler对象获取method并获取parameters,但它们似乎不包含参数的值。无论如何我可以获得在我的拦截器类中填充metadataJson的值吗?以下是我的尝试。

public class ValidateJson implements HandlerInterceptor{

@Override
public void afterCompletion(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception exception)
        throws Exception {
    // TODO Auto-generated method stub

}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler, ModelAndView modelAndView) throws Exception {
    // TODO Auto-generated method stub

}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {
    if (handler instanceof HandlerMethod) {

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        MethodParameter[] mparams = handlerMethod.getMethodParameters();
            Method method = handlerMethod.getMethod();
            Object[] allArgs = method.getParameters();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterAnnotations.length; i++) {
                Annotation[] annotations = parameterAnnotations[i];
                for(Annotation annotation: annotations) {
                    if (annotation instanceof com.fico.cardinal.cm.utils.JsonData) {
                        System.out.println(allArgs[i].toString());
                    }
                }
            }

            return true;    
    }
    return false;
} 
}

这只是打印java.lang.String arg1

0 个答案:

没有答案