Search in sources :

Example 11 with HttpMessageConverter

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

the class HttpEntityMethodProcessorTests method resolveArgumentTypeVariable.

@Test
public void resolveArgumentTypeVariable() throws Exception {
    Method method = MySimpleParameterizedController.class.getMethod("handleDto", HttpEntity.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<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    @SuppressWarnings("unchecked") HttpEntity<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(methodParam, mavContainer, webRequest, binderFactory);
    assertNotNull(result);
    assertEquals("Jad", result.getBody().getName());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod) 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) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 12 with HttpMessageConverter

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

the class HttpEntityMethodProcessorTests method resolveArgument.

@Test
public void resolveArgument() throws Exception {
    String content = "{\"name\" : \"Jad\"}";
    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<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(paramSimpleBean, mavContainer, webRequest, binderFactory);
    assertNotNull(result);
    assertEquals("Jad", result.getBody().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) Test(org.junit.Test)

Example 13 with HttpMessageConverter

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

the class HttpEntityMethodProcessorTests method resolveArgumentWithEmptyBody.

// SPR-12861
@Test
public void resolveArgumentWithEmptyBody() throws Exception {
    this.servletRequest.setContent(new byte[0]);
    this.servletRequest.setContentType("application/json");
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    HttpEntity<?> result = (HttpEntity<?>) processor.resolveArgument(this.paramSimpleBean, this.mavContainer, this.webRequest, this.binderFactory);
    assertNotNull(result);
    assertNull(result.getBody());
}
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) Test(org.junit.Test)

Example 14 with HttpMessageConverter

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

the class RequestMappingHandlerAdapterTests method responseBodyAdvice.

// SPR-10859
@Test
public void responseBodyAdvice() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    this.handlerAdapter.setMessageConverters(converters);
    this.webAppContext.registerSingleton("rba", ResponseCodeSuppressingAdvice.class);
    this.webAppContext.refresh();
    this.request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
    this.request.setParameter("c", "callback");
    HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleBadRequest");
    this.handlerAdapter.afterPropertiesSet();
    this.handlerAdapter.handle(this.request, this.response, handlerMethod);
    assertEquals(200, this.response.getStatus());
    assertEquals("{\"status\":400,\"message\":\"body\"}", this.response.getContentAsString());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 15 with HttpMessageConverter

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

the class RequestPartIntegrationTests method setup.

@Before
public void setup() {
    ByteArrayHttpMessageConverter emptyBodyConverter = new ByteArrayHttpMessageConverter();
    emptyBodyConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    List<HttpMessageConverter<?>> converters = new ArrayList<>(3);
    converters.add(emptyBodyConverter);
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new ResourceHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter());
    AllEncompassingFormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
    converter.setPartConverters(converters);
    restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    restTemplate.setMessageConverters(Collections.singletonList(converter));
}
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) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Before(org.junit.Before)

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