Search in sources :

Example 6 with MappingJackson2XmlHttpMessageConverter

use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndXmlMessageConverter.

// SPR-12149
@Test
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();
    assertThat(content.contains("<withView1>with</withView1>")).isFalse();
    assertThat(content.contains("<withView2>with</withView2>")).isTrue();
    assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
Also used : MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 7 with MappingJackson2XmlHttpMessageConverter

use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter.

// SPR-12501
@Test
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);
    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 : HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 8 with MappingJackson2XmlHttpMessageConverter

use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project group2-backend by it-kmitl-2018.

the class CustomConfiguration method configureMessageConverters.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJackson2HttpMessageConverter(Json.jsonMapper));
    converters.add(new MappingJackson2XmlHttpMessageConverter(Xml.xmlMapper));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)

Example 9 with MappingJackson2XmlHttpMessageConverter

use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project tutorials by eugenp.

the class WebConfig method configureMessageConverters.

// 
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
    final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm"));
    messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
    messageConverters.add(createXmlHttpMessageConverter());
    // messageConverters.add(new MappingJackson2HttpMessageConverter());
    messageConverters.add(new ProtobufHttpMessageConverter());
    messageConverters.add(new KryoHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    super.configureMessageConverters(messageConverters);
}
Also used : ProtobufHttpMessageConverter(org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) KryoHttpMessageConverter(org.baeldung.config.converter.KryoHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) SimpleDateFormat(java.text.SimpleDateFormat) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 10 with MappingJackson2XmlHttpMessageConverter

use of org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndXmlMessageConverter.

// SPR-12149
@Test
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();
    assertThat(content.contains("<withView1>with</withView1>")).isFalse();
    assertThat(content.contains("<withView2>with</withView2>")).isTrue();
    assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
Also used : MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)12 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)9 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)9 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)8 Test (org.junit.jupiter.api.Test)6 MethodParameter (org.springframework.core.MethodParameter)5 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)5 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)5 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)5 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 AbstractJackson2HttpMessageConverter (org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 List (java.util.List)3 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2 TimeUnit (java.util.concurrent.TimeUnit)2