Search in sources :

Example 6 with HttpMessageConverter

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

the class DelegatingWebMvcConfigurationTests method configureMessageConverters.

@Test
public void configureMessageConverters() {
    final HttpMessageConverter customConverter = mock(HttpMessageConverter.class);
    final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    List<WebMvcConfigurer> configurers = new ArrayList<>();
    configurers.add(new WebMvcConfigurerAdapter() {

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

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(0, customConverter);
        }
    });
    delegatingConfig = new DelegatingWebMvcConfiguration();
    delegatingConfig.setConfigurers(configurers);
    RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
    assertEquals("Only one custom converter should be registered", 2, adapter.getMessageConverters().size());
    assertSame(customConverter, adapter.getMessageConverters().get(0));
    assertSame(stringConverter, adapter.getMessageConverters().get(1));
}
Also used : StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 7 with HttpMessageConverter

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

the class TestPathHelper method verifyMessageConverters.

@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
    assertNotNull(bean);
    Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
    assertNotNull(value);
    assertTrue(value instanceof List);
    List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
    if (hasDefaultRegistrations) {
        assertTrue("Default and custom converter expected", converters.size() > 2);
    } else {
        assertTrue("Only custom converters expected", converters.size() == 2);
    }
    assertTrue(converters.get(0) instanceof StringHttpMessageConverter);
    assertTrue(converters.get(1) instanceof ResourceHttpMessageConverter);
}
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 8 with HttpMessageConverter

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

the class HttpEntityMethodProcessorMockTests method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    stringHttpMessageConverter = mock(HttpMessageConverter.class);
    given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resourceMessageConverter = mock(HttpMessageConverter.class);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(stringHttpMessageConverter);
    converters.add(resourceMessageConverter);
    processor = new HttpEntityMethodProcessor(converters);
    reset(stringHttpMessageConverter);
    reset(resourceMessageConverter);
    Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class);
    paramHttpEntity = new MethodParameter(handle1, 0);
    paramRequestEntity = new MethodParameter(handle1, 3);
    paramResponseEntity = new MethodParameter(handle1, 1);
    paramInt = new MethodParameter(handle1, 2);
    returnTypeResponseEntity = new MethodParameter(handle1, -1);
    returnTypeResponseEntityProduces = new MethodParameter(getClass().getMethod("handle4"), -1);
    returnTypeHttpEntity = new MethodParameter(getClass().getMethod("handle2", HttpEntity.class), -1);
    returnTypeHttpEntitySubclass = new MethodParameter(getClass().getMethod("handle2x", HttpEntity.class), -1);
    returnTypeInt = new MethodParameter(getClass().getMethod("handle3"), -1);
    returnTypeResponseEntityResource = new MethodParameter(getClass().getMethod("handle5"), -1);
    mavContainer = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest("GET", "/foo");
    servletResponse = new MockHttpServletResponse();
    webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) MethodParameter(org.springframework.core.MethodParameter) SimpleDateFormat(java.text.SimpleDateFormat) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 9 with HttpMessageConverter

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

the class HttpEntityMethodProcessorTests method resolveGenericArgument.

@Test
public void resolveGenericArgument() throws Exception {
    String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
    this.servletRequest.setContent(content.getBytes("UTF-8"));
    this.servletRequest.setContentType("application/json");
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    @SuppressWarnings("unchecked") HttpEntity<List<SimpleBean>> result = (HttpEntity<List<SimpleBean>>) processor.resolveArgument(paramList, mavContainer, webRequest, binderFactory);
    assertNotNull(result);
    assertEquals("Jad", result.getBody().get(0).getName());
    assertEquals("Robert", result.getBody().get(1).getName());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 10 with HttpMessageConverter

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

the class HttpEntityMethodProcessorTests method jacksonTypeInfoList.

// SPR-12811
@Test
public void jacksonTypeInfoList() throws Exception {
    Method method = JacksonController.class.getMethod("handleList");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    Object returnValue = new JacksonController().handleList();
    processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);
    String content = this.servletResponse.getContentAsString();
    assertTrue(content.contains("\"type\":\"foo\""));
    assertTrue(content.contains("\"type\":\"bar\""));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) 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.Test)

Aggregations

HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)91 ArrayList (java.util.ArrayList)65 Test (org.junit.Test)62 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)57 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)49 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)37 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)32 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)32 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)30 RestTemplate (org.springframework.web.client.RestTemplate)29 Method (java.lang.reflect.Method)25 MethodParameter (org.springframework.core.MethodParameter)25 HandlerMethod (org.springframework.web.method.HandlerMethod)24 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)14 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)13 URI (java.net.URI)13 HashMap (java.util.HashMap)13 MapPropertySource (org.springframework.core.env.MapPropertySource)13 StandardEnvironment (org.springframework.core.env.StandardEnvironment)13