/** * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); }
/** * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); }
private HandlerResult getHandlerResult(Object controller, Method method) { HandlerMethod handlerMethod = new HandlerMethod(controller, method); return new HandlerResult(handlerMethod, null, handlerMethod.getReturnType()); }
@Test // SPR-12811 public void jacksonTypeInfoList() throws Exception { Method method = JacksonController.class.getMethod("handleTypeInfoList"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleTypeInfoList(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"type\":\"foo\"")); assertTrue(content.contains("\"type\":\"bar\"")); }
@Test // SPR-13318 public void jacksonSubType() throws Exception { Method method = JacksonController.class.getMethod("handleSubType"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleSubType(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"id\":123")); assertTrue(content.contains("\"name\":\"foo\"")); }
@Test // SPR-12811 public void jacksonTypeInfoList() throws Exception { Method method = JacksonController.class.getMethod("handleList"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters); Object returnValue = new JacksonController().handleList(); processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"type\":\"foo\"")); assertTrue(content.contains("\"type\":\"bar\"")); }
@Test public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() throws Exception { Method method = JacksonController.class.getMethod("handleResponseBody"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor( converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice())); Object returnValue = new JacksonController().handleResponseBody(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertFalse(content.contains("\"withView1\":\"with\"")); assertTrue(content.contains("\"withView2\":\"with\"")); assertFalse(content.contains("\"withoutView\":\"without\"")); }
@Test public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception { Method method = JacksonController.class.getMethod("handleResponseEntity"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor( converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice())); Object returnValue = new JacksonController().handleResponseEntity(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertFalse(content.contains("\"withView1\":\"with\"")); assertTrue(content.contains("\"withView2\":\"with\"")); assertFalse(content.contains("\"withoutView\":\"without\"")); }
@Test // SPR-13631 public void defaultCharset() throws Exception { Method method = JacksonController.class.getMethod("defaultCharset"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().defaultCharset(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); assertEquals("UTF-8", this.servletResponse.getCharacterEncoding()); }
@Test // SPR-13318 public void jacksonSubTypeList() throws Exception { Method method = JacksonController.class.getMethod("handleSubTypeList"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleSubTypeList(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"id\":123")); assertTrue(content.contains("\"name\":\"foo\"")); assertTrue(content.contains("\"id\":456")); assertTrue(content.contains("\"name\":\"bar\"")); }
@Test // SPR-12149 public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exception { Method method = JacksonController.class.getMethod("handleResponseEntity"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2XmlHttpMessageConverter()); HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor( converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice())); Object returnValue = new JacksonController().handleResponseEntity(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertFalse(content.contains("<withView1>with</withView1>")); assertTrue(content.contains("<withView2>with</withView2>")); assertFalse(content.contains("<withoutView>without</withoutView>")); }
@Test // SPR-12149 public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Exception { Method method = JacksonController.class.getMethod("handleResponseBody"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2XmlHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor( converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice())); Object returnValue = new JacksonController().handleResponseBody(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertFalse(content.contains("<withView1>with</withView1>")); assertTrue(content.contains("<withView2>with</withView2>")); assertFalse(content.contains("<withoutView>without</withoutView>")); }
/** * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); }
/** * Return {@code true} if the method return type is void, {@code false} otherwise. */ public boolean isVoid() { return Void.TYPE.equals(getReturnType().getParameterType()); }
private String modelName(CatnapResponseBody annotation, HandlerMethod method) { if (!StringUtils.isEmpty(annotation.value())) { return annotation.value(); } else { if (List.class.isAssignableFrom(method.getReturnType().getParameterType())) { String paramType = method.getReturnType().getGenericParameterType().getTypeName(); paramType = paramType.substring(paramType.lastIndexOf(".") + 1, paramType.length() - 1); return Introspector.decapitalize(paramType) + "List"; } if (Map.class.isAssignableFrom(method.getReturnType().getParameterType())) { throw new ViewRenderException("Map is not a supported return type for methods annotated with " + "@CatnapResponseBody. Please return an object or a list of objects. If you wish to return " + "a map then you must use Catnap's standard model and view handling by removing the " + "@CatnapResponseBody annotation."); } return Introspector.decapitalize(method.getReturnType().getParameterType().getSimpleName()); } } }
private String modelName(CatnapResponseBody annotation, HandlerMethod method) { if (!StringUtils.isEmpty(annotation.value())) { return annotation.value(); } else { if (List.class.isAssignableFrom(method.getReturnType().getParameterType())) { String paramType = method.getReturnType().getGenericParameterType().getTypeName(); paramType = paramType.substring(paramType.lastIndexOf(".") + 1, paramType.length() - 1); return Introspector.decapitalize(paramType) + "List"; } if (Map.class.isAssignableFrom(method.getReturnType().getParameterType())) { throw new ViewRenderException("Map is not a supported return type for methods annotated with " + "@CatnapResponseBody. Please return an object or a list of objects. If you wish to return " + "a map then you must use Catnap's standard model and view handling by removing the " + "@CatnapResponseBody annotation."); } return Introspector.decapitalize(method.getReturnType().getParameterType().getSimpleName()); } } }
@Override public Object invoke() { Map<String, ProtoFileResourceUtils.ProtoFileInfo> protoFileInfoMap = new HashMap<>(); Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); handlerMethods.forEach((requestMappingInfo, handlerMethod) -> { Class<?> raptorInterface = RaptorInterfaceUtils.getInterfaceClass(handlerMethod.getBeanType(), handlerMethod.getMethod()); if (raptorInterface != null) { ProtoFileResourceUtils.ProtoFileInfo protoFileInfo = ProtoFileResourceUtils.findInterfaceProtoFile(raptorInterface, classLoader); if (protoFileInfo != null) { protoFileInfoMap.putIfAbsent(protoFileInfo.getFullName(), protoFileInfo); } } MethodParameter[] methodParameters = handlerMethod.getMethodParameters(); List<Class<?>> messageClasses = Arrays.stream(methodParameters).map(MethodParameter::getParameterType).collect(Collectors.toList()); MethodParameter methodReturnType = handlerMethod.getReturnType(); messageClasses.add(methodReturnType.getParameterType()); List<ProtoFileResourceUtils.ProtoFileInfo> messageProtoFiles = ProtoFileResourceUtils.findProtoFilesByMessage(messageClasses, classLoader); messageProtoFiles.forEach(protoFileInfo -> protoFileInfoMap.putIfAbsent(protoFileInfo.getFullName(), protoFileInfo)); }); return protoFileInfoMap; } }
/** * springmvc提供的render方法 * */ public void springMvcRender(Object returnValue){ try{ // modelAndViewContainer init ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer(); ServletWebRequest webRequest = new ServletWebRequest(AoomsContext.getRequest(), AoomsContext.getResponse()); // get ApplicationContext ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(AoomsContext.getServletContext()); // get RequestMappingHandlerAdapter Bean RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter)ac1.getBean("requestMappingHandlerAdapter"); // init handlerMethodReturnValueHandlerComposite HandlerMethodReturnValueHandlerComposite handlerMethodReturnValueHandlerComposite = new HandlerMethodReturnValueHandlerComposite(); handlerMethodReturnValueHandlerComposite.addHandlers(requestMappingHandlerAdapter.getReturnValueHandlers()); // render HandlerMethod method = AoomsContext.getMethodHandler(); handlerMethodReturnValueHandlerComposite.handleReturnValue(returnValue, method.getReturnType(), modelAndViewContainer, webRequest); } catch (Exception ex){ throw AoomsExceptions.create("springmvc render error",ex); } };