Search in sources :

Example 96 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class XmlContentRequestMatchersIntegrationTests method setup.

@BeforeEach
public void setup() {
    List<Person> composers = Arrays.asList(new Person("Johann Sebastian Bach").setSomeDouble(21), new Person("Johannes Brahms").setSomeDouble(.0025), new Person("Edvard Grieg").setSomeDouble(1.6035), new Person("Robert Schumann").setSomeDouble(Double.NaN));
    this.people = new PeopleWrapper(composers);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new Jaxb2RootElementHttpMessageConverter());
    this.restTemplate = new RestTemplate();
    this.restTemplate.setMessageConverters(converters);
    this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
Also used : Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) Person(org.springframework.test.web.Person) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 97 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class XpathRequestMatchersIntegrationTests method setup.

@BeforeEach
public void setup() {
    List<Person> composers = Arrays.asList(new Person("Johann Sebastian Bach").setSomeDouble(21), new Person("Johannes Brahms").setSomeDouble(.0025), new Person("Edvard Grieg").setSomeDouble(1.6035), new Person("Robert Schumann").setSomeDouble(Double.NaN));
    List<Person> performers = Arrays.asList(new Person("Vladimir Ashkenazy").setSomeBoolean(false), new Person("Yehudi Menuhin").setSomeBoolean(true));
    this.people = new PeopleWrapper(composers, performers);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new Jaxb2RootElementHttpMessageConverter());
    this.restTemplate = new RestTemplate();
    this.restTemplate.setMessageConverters(converters);
    this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
Also used : Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) Person(org.springframework.test.web.Person) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 98 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class RouterFunctionMapping method initMessageConverters.

/**
 * Initializes a default set of {@linkplain HttpMessageConverter message converters}.
 */
private void initMessageConverters() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(4);
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    try {
        messageConverters.add(new SourceHttpMessageConverter<>());
    } catch (Error err) {
    // Ignore when no TransformerFactory implementation is available
    }
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    this.messageConverters = messageConverters;
}
Also used : 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) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 99 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class TestPathHelper method verifyMessageConverters.

@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
    assertThat(bean).isNotNull();
    Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
    assertThat(value).isNotNull();
    assertThat(value instanceof List).isTrue();
    List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
    if (hasDefaultRegistrations) {
        assertThat(converters.size() > 2).as("Default and custom converter expected").isTrue();
    } else {
        assertThat(converters.size() == 2).as("Only custom converters expected").isTrue();
    }
    assertThat(converters.get(0) instanceof StringHttpMessageConverter).isTrue();
    assertThat(converters.get(1) instanceof ResourceHttpMessageConverter).isTrue();
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) List(java.util.List) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 100 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class ServletAnnotationControllerHandlerMethodTests method produces.

@Test
public void produces() throws Exception {
    initServlet(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);
    }, ProducesController.class);
    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) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) 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) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)246 ArrayList (java.util.ArrayList)162 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)150 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)142 Test (org.junit.jupiter.api.Test)98 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)90 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)68 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)67 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)67 RestTemplate (org.springframework.web.client.RestTemplate)64 HandlerMethod (org.springframework.web.method.HandlerMethod)52 MethodParameter (org.springframework.core.MethodParameter)51 Method (java.lang.reflect.Method)48 Test (org.junit.Test)30 MediaType (org.springframework.http.MediaType)23 List (java.util.List)19 Bean (org.springframework.context.annotation.Bean)19 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)16 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15