use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndXmlMessageConverter.
// SPR-12149
@Test
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Throwable {
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(request, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("<withView1>with</withView1>")).isFalse();
assertThat(content.contains("<withView2>with</withView2>")).isTrue();
assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method supportsReturnTypeResponseBodyOnType.
@Test
public void supportsReturnTypeResponseBodyOnType() throws Throwable {
Method method = ResponseBodyController.class.getMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
assertThat(processor.supportsHandlerMethod(new HandlerMethod(this, method))).as("Failed to recognize type-level @ResponseBody").isTrue();
}
use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method jacksonSubType.
// SPR-13318
@Test
public void jacksonSubType() throws Throwable {
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(request, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("\"id\":123")).isTrue();
assertThat(content.contains("\"name\":\"foo\"")).isTrue();
}
use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndXmlMessageConverter.
// SPR-12149
@Test
public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Throwable {
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()), null);
Object returnValue = new JacksonController().handleResponseEntity();
processor.handleReturnValue(request, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("<withView1>with</withView1>")).isFalse();
assertThat(content.contains("<withView2>with</withView2>")).isTrue();
assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithNonGenericConverter.
// SPR-11225
@Test
public void resolveArgumentTypeVariableWithNonGenericConverter() throws Throwable {
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(StandardCharsets.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(request, new ResolvableMethodParameter(methodParam));
assertThat(result).isNotNull();
assertThat(result.getName()).isEqualTo("Jad");
}
Aggregations