Search in sources :

Example 26 with HttpMessageConverter

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentRequiredNoContent.

// SPR-9942
@Test(expected = HttpMessageNotReadableException.class)
public void resolveArgumentRequiredNoContent() throws Exception {
    this.servletRequest.setContent(new byte[0]);
    this.servletRequest.setContentType("text/plain");
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new StringHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.resolveArgument(paramString, container, request, factory);
}
Also used : ArrayList(java.util.ArrayList) 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) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 27 with HttpMessageConverter

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithGenericInterface.

// SPR-14520
@Test
public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {
    this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
    Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    String value = (String) processor.readWithMessageConverters(this.request, methodParameter, methodParameter.getGenericParameterType());
    assertEquals("foo", value);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) 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) 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)

Example 28 with HttpMessageConverter

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

the class RequestResponseBodyMethodProcessorTests method handleReturnValueStringAcceptCharset.

@Test
public void handleReturnValueStringAcceptCharset() throws Exception {
    this.servletRequest.addHeader("Accept", "text/plain;charset=UTF-8");
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.writeWithMessageConverters("Foo", returnTypeString, request);
    assertEquals("text/plain;charset=UTF-8", servletResponse.getHeader("Content-Type"));
}
Also used : ArrayList(java.util.ArrayList) 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) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 29 with HttpMessageConverter

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

the class RequestResponseBodyMethodProcessorTests method handleReturnValueSortByQuality.

// SPR-9160
@Test
public void handleReturnValueSortByQuality() throws Exception {
    this.servletRequest.addHeader("Accept", "text/plain; q=0.5, application/json");
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.writeWithMessageConverters("Foo", returnTypeString, request);
    assertEquals("application/json;charset=UTF-8", servletResponse.getHeader("Content-Type"));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) 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) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 30 with HttpMessageConverter

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithNonGenericConverter.

// SPR-11225
@Test
public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exception {
    Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    String content = "{\"name\" : \"Jad\"}";
    this.servletRequest.setContent(content.getBytes("UTF-8"));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    HttpMessageConverter target = new MappingJackson2HttpMessageConverter();
    HttpMessageConverter proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target));
    converters.add(proxy);
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    SimpleBean result = (SimpleBean) processor.resolveArgument(methodParam, container, request, factory);
    assertNotNull(result);
    assertEquals("Jad", result.getName());
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) 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) 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