use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentClassJson.
@Test
public void resolveArgumentClassJson() 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());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
SimpleBean result = (SimpleBean) processor.resolveArgument(paramSimpleBean, container, request, factory);
assertNotNull(result);
assertEquals("Jad", result.getName());
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndXmlMessageConverter.
// SPR-12149
@Test
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Exception {
Method method = JacksonController.class.getMethod("handleResponseBody");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2XmlHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
Object returnValue = new JacksonController().handleResponseBody();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("<withView1>with</withView1>"));
assertTrue(content.contains("<withView2>with</withView2>"));
assertFalse(content.contains("<withoutView>without</withoutView>"));
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method supportsReturnTypeResponseBodyOnType.
@Test
public void supportsReturnTypeResponseBodyOnType() throws Exception {
Method method = ResponseBodyController.class.getMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
assertTrue("Failed to recognize type-level @ResponseBody", processor.supportsReturnType(returnType));
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentWithJacksonJsonViewAndXmlMessageConverter.
// SPR-12501
@Test
public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Exception {
String content = "<root><withView1>with</withView1><withView2>with</withView2><withoutView>without</withoutView></root>";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2XmlHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice()));
@SuppressWarnings("unchecked") JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(methodParameter, this.container, this.request, this.factory);
assertNotNull(result);
assertEquals("with", result.getWithView1());
assertNull(result.getWithView2());
assertNull(result.getWithoutView());
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method handleReturnValueCharSequence.
// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
Method method = ResponseBodyController.class.getMethod("handleWithCharSequence");
MethodParameter returnType = new MethodParameter(method, -1);
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
processor.handleReturnValue(new StringBuilder("Foo"), returnType, container, request);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
Aggregations