Search in sources :

Example 31 with StringHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method overlappingMessageConvertersRequestBody.

// SPR-6877
@PathPatternsParameterizedTest
void overlappingMessageConvertersRequestBody(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new StringHttpMessageConverter());
        messageConverters.add(new SimpleMessageConverter(new MediaType("application", "json"), MediaType.ALL));
        adapterDef.getPropertyValues().add("messageConverters", messageConverters);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    request.setContent("Hello World".getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "application/json, text/javascript, */*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getHeader("Content-Type")).as("Invalid content-type").isEqualTo("application/json");
}
Also used : 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) MediaType(org.springframework.http.MediaType) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 32 with StringHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method responseBodyNoAcceptableMediaType.

@PathPatternsParameterizedTest
void responseBodyNoAcceptableMediaType(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyProducesController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        adapterDef.getPropertyValues().add("messageConverters", converter);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "application/pdf, application/msword");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(406);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 33 with StringHttpMessageConverter

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

the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.

@PathPatternsParameterizedTest
void unsupportedRequestBody(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
        adapterDef.getPropertyValues().add("messageConverters", converter);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "application/pdf");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(415);
    assertThat(response.getHeader("Accept")).isEqualTo("text/plain");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 34 with StringHttpMessageConverter

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

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 35 with StringHttpMessageConverter

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

the class DelegatingWebMvcConfigurationTests method configureMessageConverters.

@Test
public void configureMessageConverters() {
    HttpMessageConverter<?> customConverter = mock(HttpMessageConverter.class);
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    WebMvcConfigurer configurer = new WebMvcConfigurer() {

        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(stringConverter);
        }

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(0, customConverter);
        }
    };
    webMvcConfig.setConfigurers(Collections.singletonList(configurer));
    RequestMappingHandlerAdapter adapter = webMvcConfig.requestMappingHandlerAdapter(this.webMvcConfig.mvcContentNegotiationManager(), this.webMvcConfig.mvcConversionService(), this.webMvcConfig.mvcValidator());
    assertThat(adapter.getMessageConverters().size()).as("One custom converter expected").isEqualTo(2);
    assertThat(adapter.getMessageConverters().get(0)).isSameAs(customConverter);
    assertThat(adapter.getMessageConverters().get(1)).isSameAs(stringConverter);
}
Also used : List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)91 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)46 ArrayList (java.util.ArrayList)38 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)24 RestTemplate (org.springframework.web.client.RestTemplate)23 Test (org.junit.jupiter.api.Test)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)18 Test (org.junit.Test)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)11 MediaType (org.springframework.http.MediaType)9 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)9 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)9 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 URI (java.net.URI)7 HashMap (java.util.HashMap)7 Before (org.junit.Before)7 MethodParameter (org.springframework.core.MethodParameter)7