/** Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findModelAttributes(HandlerMethod handlerMethod, SessionAttributesHandler sessionAttributesHandler) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
/** * Find {@code @ModelAttribute} arguments also listed as {@code @SessionAttributes}. */ private List<String> findSessionAttributeArguments(HandlerMethod handlerMethod) { List<String> result = new ArrayList<>(); for (MethodParameter parameter : handlerMethod.getMethodParameters()) { if (parameter.hasParameterAnnotation(ModelAttribute.class)) { String name = getNameForParameter(parameter); Class<?> paramType = parameter.getParameterType(); if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { result.add(name); } } } return result; }
@Test // SPR-14520 public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); String value = (String) processor.readWithMessageConverters( this.request, methodParameter, methodParameter.getGenericParameterType()); assertEquals("foo", value); }
@Test // SPR-11225 public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exception { Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class); HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List<HttpMessageConverter<?>> converters = new ArrayList<>(); HttpMessageConverter<Object> target = new MappingJackson2HttpMessageConverter(); HttpMessageConverter<?> proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target)); converters.add(proxy); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); SimpleBean result = (SimpleBean) processor.resolveArgument(methodParam, container, request, factory); assertNotNull(result); assertEquals("Jad", result.getName()); }
@Test // SPR-9964 public void resolveArgumentTypeVariable() throws Exception { Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class); HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); SimpleBean result = (SimpleBean) processor.resolveArgument(methodParam, container, request, factory); assertNotNull(result); assertEquals("Jad", result.getName()); }
@Test // SPR-14470 public void resolveParameterizedWithTypeVariableArgument() throws Exception { Method method = MyParameterizedControllerWithList.class.getMethod("handleDto", List.class); HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedControllerWithList(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); @SuppressWarnings("unchecked") List<SimpleBean> result = (List<SimpleBean>) processor.resolveArgument( methodParam, container, request, factory); assertNotNull(result); assertEquals("Jad", result.get(0).getName()); assertEquals("Robert", result.get(1).getName()); }
@Test // SPR-9964 public void parameterizedMethodArgument() throws Exception { Method method = AbstractParameterizedController.class.getMethod("handleDto", Identifiable.class); HandlerMethod handlerMethod = new HandlerMethod(new ConcreteParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; SimpleBean simpleBean = resolveValue(methodParam, "{\"name\" : \"Jad\"}"); assertEquals("Jad", simpleBean.getName()); }
@Test public void resolveArgumentTypeVariable() throws Exception { Method method = MySimpleParameterizedController.class.getMethod("handleDto", HttpEntity.class); HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method); MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters); @SuppressWarnings("unchecked") HttpEntity<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(methodParam, mavContainer, webRequest, binderFactory); assertNotNull(result); assertEquals("Jad", result.getBody().getName()); }
@Test // SPR-12501 public void resolveArgumentWithJacksonJsonView() throws Exception { String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor( converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice())); @SuppressWarnings("unchecked") JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(methodParameter, this.container, this.request, this.factory); assertNotNull(result); assertEquals("with", result.getWithView1()); assertNull(result.getWithView2()); assertNull(result.getWithoutView()); }
@Test // SPR-12501 public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Exception { String content = "<root>" + "<withView1>with</withView1>" + "<withView2>with</withView2>" + "<withoutView>without</withoutView></root>"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2XmlHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor( converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice())); @SuppressWarnings("unchecked") JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(methodParameter, this.container, this.request, this.factory); assertNotNull(result); assertEquals("with", result.getWithView1()); assertNull(result.getWithView2()); assertNull(result.getWithoutView()); }
@Test // SPR-12501 public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception { String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor( converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice())); @SuppressWarnings("unchecked") HttpEntity<JacksonViewBean> result = (HttpEntity<JacksonViewBean>) processor.resolveArgument( methodParameter, this.container, this.request, this.factory); assertNotNull(result); assertNotNull(result.getBody()); assertEquals("with", result.getBody().getWithView1()); assertNull(result.getBody().getWithView2()); assertNull(result.getBody().getWithoutView()); }
@Test // SPR-12501 public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Exception { String content = "<root>" + "<withView1>with</withView1>" + "<withView2>with</withView2>" + "<withoutView>without</withoutView></root>"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2XmlHttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor( converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice())); @SuppressWarnings("unchecked") HttpEntity<JacksonViewBean> result = (HttpEntity<JacksonViewBean>) processor.resolveArgument(methodParameter, this.container, this.request, this.factory); assertNotNull(result); assertNotNull(result.getBody()); assertEquals("with", result.getBody().getWithView1()); assertNull(result.getBody().getWithView2()); assertNull(result.getBody().getWithoutView()); }
@Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { // 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) // System.out.println("CustomInterceptor1.postHandle:" + o + ", modelAndView:" + modelAndView); HandlerMethod method = (HandlerMethod) o; System.out.println("-- MethodName:" + method.getMethod().getName()); System.out.println("-- ReturnType:" + method.getMethod().getReturnType()); System.out.println("-- MethodParameters:" + method.getMethodParameters()); MethodParameter[] parameters = method.getMethodParameters(); if (null != parameters) { for (MethodParameter parameter : parameters) { System.out.println(" -- parameterIndex:" + parameter.getParameterIndex() + ",parameterName:" + parameter.getParameterName() + ",parameterType:" + parameter.getParameterType()); } } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod = (HandlerMethod) handler; MethodParameter[] methodParameters = handlerMethod.getMethodParameters(); // Inspect & log parameters... }
/** * convert parameter names of the method into a String value<br> * <br> * @param handlerMethod Target methods of interceptor * @return String parameter string */ protected static String buildMethodParams(HandlerMethod handlerMethod) { MethodParameter[] params = handlerMethod.getMethodParameters(); List<String> lst = new ArrayList<String>(params.length); for (MethodParameter p : params) { lst.add(p.getParameterType().getSimpleName()); } return StringUtils.collectionToCommaDelimitedString(lst); }
@ExceptionHandler(ConstraintViolationException.class) public Result constraintViolationExceptionHandler(ConstraintViolationException ex, HttpServletResponse response, HandlerMethod handlerMethod) { return paramValidExceptionHandler(new ParamValidException(ex, handlerMethod.getMethodParameters()), response); }
@Override public List<ResolvedMethodParameter> apply(ResolvedMethod input) { List<ResolvedMethodParameter> parameters = newArrayList(); MethodParameter[] methodParameters = methodToResolve.getMethodParameters(); for (int i = 0; i < input.getArgumentCount(); i++) { parameters.add(new ResolvedMethodParameter( discoveredName(methodParameters[i]).or(String.format("param%s", i)), methodParameters[i], input.getArgumentType(i))); } return parameters; } };
public boolean hasPassportParameter(HandlerMethod handlerMethod) { MethodParameter[] parameters = handlerMethod.getMethodParameters(); if (parameters != null) { for (MethodParameter parameter : parameters) { parameter.initParameterNameDiscovery(parameterNameDiscoverer); String parameterName = parameter.getParameterName(); if (parameterNameMap.containsKey(parameterName)) { // logger.info("hasPassportParameter parameterName:" + parameterName); return true; } } } return false; }
@Test public void preHandleForNotBotMessageHandler() throws Exception { HandlerMethod handler = mock(HandlerMethod.class); when(handler.getMethodParameters()).thenReturn(new MethodParameter[] {}); boolean result = target.preHandle(request, response, handler); assertThat(result).isTrue(); } }