Search in sources :

Example 1 with Jaxb2RootElementHttpMessageConverter

use of org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter in project earth-snow by justlive1.

the class WebConfig method xmlConverter.

@Bean
public Jaxb2RootElementHttpMessageConverter xmlConverter() {
    Jaxb2RootElementHttpMessageConverter converter = new Jaxb2RootElementHttpMessageConverter();
    converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_XML));
    return converter;
}
Also used : Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 2 with Jaxb2RootElementHttpMessageConverter

use of org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter in project molgenis by molgenis.

the class HttpClientConfig method converters.

private List<HttpMessageConverter<?>> converters() {
    List<HttpMessageConverter<?>> result = Lists.newArrayList();
    result.add(new ByteArrayHttpMessageConverter());
    result.add(new StringHttpMessageConverter());
    result.add(new ResourceHttpMessageConverter());
    result.add(new SourceHttpMessageConverter<>());
    result.add(new AllEncompassingFormHttpMessageConverter());
    result.add(new Jaxb2RootElementHttpMessageConverter());
    result.add(gsonHttpMessageConverter);
    return result;
}
Also used : Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) SourceHttpMessageConverter(org.springframework.http.converter.xml.SourceHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 3 with Jaxb2RootElementHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method httpEntityWithContentType.

// SPR-16172
@PathPatternsParameterizedTest
void httpEntityWithContentType(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ResponseEntityController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
        adapterDef.getPropertyValues().add("messageConverters", messageConverters);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-entity");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getHeader("Content-Type")).isEqualTo("application/xml");
    assertThat(response.getContentAsString()).isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<testEntity><name>Foo Bar</name></testEntity>");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 4 with Jaxb2RootElementHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method produces.

@PathPatternsParameterizedTest
void produces(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ProducesController.class, usePathPatterns, wac -> {
        List<HttpMessageConverter<?>> converters = new ArrayList<>();
        converters.add(new MappingJackson2HttpMessageConverter());
        converters.add(new Jaxb2RootElementHttpMessageConverter());
        RootBeanDefinition beanDef;
        beanDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        beanDef.getPropertyValues().add("messageConverters", converters);
        wac.registerBeanDefinition("handlerAdapter", beanDef);
        beanDef = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
        beanDef.getPropertyValues().add("messageConverters", converters);
        wac.registerBeanDefinition("requestMappingResolver", beanDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "text/html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("html");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/xml");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/xml, text/html");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "text/html;q=0.9, application/xml");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/msword");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(406);
    // SPR-16318
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "text/csv,application/problem+json");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(500);
    assertThat(response.getContentType()).isEqualTo("application/problem+json");
    assertThat(response.getContentAsString()).isEqualTo("{\"reason\":\"error\"}");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 5 with Jaxb2RootElementHttpMessageConverter

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

the class WebMvcConfigurationSupport method addDefaultHttpMessageConverters.

/**
 * Adds a set of default HttpMessageConverter instances to the given list.
 * Subclasses can call this method from {@link #configureMessageConverters}.
 * @param messageConverters the list to add the default message converters to
 */
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new ResourceHttpMessageConverter());
    messageConverters.add(new ResourceRegionHttpMessageConverter());
    if (!shouldIgnoreXml) {
        try {
            messageConverters.add(new SourceHttpMessageConverter<>());
        } catch (Throwable ex) {
        // Ignore when no TransformerFactory implementation is available...
        }
    }
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    if (romePresent) {
        messageConverters.add(new AtomFeedHttpMessageConverter());
        messageConverters.add(new RssChannelHttpMessageConverter());
    }
    if (!shouldIgnoreXml) {
        if (jackson2XmlPresent) {
            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
            if (this.applicationContext != null) {
                builder.applicationContext(this.applicationContext);
            }
            messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
        } else if (jaxb2Present) {
            messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
        }
    }
    if (kotlinSerializationJsonPresent) {
        messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
    }
    if (jackson2Present) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
        if (this.applicationContext != null) {
            builder.applicationContext(this.applicationContext);
        }
        messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    } else if (gsonPresent) {
        messageConverters.add(new GsonHttpMessageConverter());
    } else if (jsonbPresent) {
        messageConverters.add(new JsonbHttpMessageConverter());
    }
    if (jackson2SmilePresent) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();
        if (this.applicationContext != null) {
            builder.applicationContext(this.applicationContext);
        }
        messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));
    }
    if (jackson2CborPresent) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();
        if (this.applicationContext != null) {
            builder.applicationContext(this.applicationContext);
        }
        messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));
    }
}
Also used : Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ResourceRegionHttpMessageConverter(org.springframework.http.converter.ResourceRegionHttpMessageConverter) AtomFeedHttpMessageConverter(org.springframework.http.converter.feed.AtomFeedHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2SmileHttpMessageConverter(org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter) JsonbHttpMessageConverter(org.springframework.http.converter.json.JsonbHttpMessageConverter) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) KotlinSerializationJsonHttpMessageConverter(org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter) RssChannelHttpMessageConverter(org.springframework.http.converter.feed.RssChannelHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) MappingJackson2CborHttpMessageConverter(org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter)

Aggregations

Jaxb2RootElementHttpMessageConverter (org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter)8 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)5 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)5 ArrayList (java.util.ArrayList)4 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)4 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)3 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)3 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)2 MarshallingHttpMessageConverter (org.springframework.http.converter.xml.MarshallingHttpMessageConverter)2 Person (org.springframework.test.web.Person)2 RestTemplate (org.springframework.web.client.RestTemplate)2 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Bean (org.springframework.context.annotation.Bean)1