use of cn.taketoday.http.converter.HttpMessageConverter 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.http.converter.HttpMessageConverter 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");
}
use of cn.taketoday.http.converter.HttpMessageConverter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter.
// SPR-12501
@Test
public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Throwable {
String content = "<root>" + "<withView1>with</withView1>" + "<withView2>with</withView2>" + "<withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes(StandardCharsets.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()), null);
@SuppressWarnings("unchecked") HttpEntity<JacksonViewBean> result = (HttpEntity<JacksonViewBean>) processor.resolveArgument(request, new ResolvableMethodParameter(methodParameter));
assertThat(result).isNotNull();
assertThat(result.getBody()).isNotNull();
assertThat(result.getBody().getWithView1()).isEqualTo("with");
assertThat(result.getBody().getWithView2()).isNull();
assertThat(result.getBody().getWithoutView()).isNull();
}
use of cn.taketoday.http.converter.HttpMessageConverter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithGenericInterface.
// SPR-14520
@Test
public void resolveArgumentTypeVariableWithGenericInterface() throws Throwable {
this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_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);
assertThat(processor.supportsParameter(new ResolvableMethodParameter(methodParameter))).isTrue();
String value = (String) processor.readWithMessageConverters(this.request, methodParameter, methodParameter.getGenericParameterType());
assertThat(value).isEqualTo("foo");
}
use of cn.taketoday.http.converter.HttpMessageConverter in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentRawTypeFromParameterizedType.
@Test
public void resolveArgumentRawTypeFromParameterizedType() throws Throwable {
String content = "fruit=apple&vegetable=kale";
this.servletRequest.setMethod("GET");
this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new AllEncompassingFormHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
@SuppressWarnings("unchecked") MultiValueMap<String, String> result = (MultiValueMap<String, String>) processor.resolveArgument(request, paramMultiValueMap);
assertThat(result).isNotNull();
assertThat(result.getFirst("fruit")).isEqualTo("apple");
assertThat(result.getFirst("vegetable")).isEqualTo("kale");
}
Aggregations