Search in sources :

Example 1 with MappingJackson2XmlHttpMessageConverter

use of cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter 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();
}
Also used : MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) JsonViewResponseBodyAdvice(cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 2 with MappingJackson2XmlHttpMessageConverter

use of cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter 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();
}
Also used : MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) JsonViewResponseBodyAdvice(cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 3 with MappingJackson2XmlHttpMessageConverter

use of cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter 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();
}
Also used : JsonViewRequestBodyAdvice(cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice) HttpEntity(cn.taketoday.http.HttpEntity) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 4 with MappingJackson2XmlHttpMessageConverter

use of cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentWithJacksonJsonViewAndXmlMessageConverter.

// SPR-12501
@Test
public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() 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("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()));
    JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParameter));
    assertThat(result).isNotNull();
    assertThat(result.getWithView1()).isEqualTo("with");
    assertThat(result.getWithView2()).isNull();
    assertThat(result.getWithoutView()).isNull();
}
Also used : JsonViewRequestBodyAdvice(cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

MethodParameter (cn.taketoday.core.MethodParameter)4 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)4 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)4 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)4 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)4 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)4 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)4 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)4 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)4 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)4 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 Test (org.junit.jupiter.api.Test)4 JsonViewRequestBodyAdvice (cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice)2 JsonViewResponseBodyAdvice (cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice)2 HttpEntity (cn.taketoday.http.HttpEntity)1