use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method handleReturnValueWithObjectMapperByTypeRegistration.
// gh-26212
@Test
public void handleReturnValueWithObjectMapperByTypeRegistration() throws Exception {
MediaType halFormsMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
MediaType halMediaType = MediaType.parseMediaType("application/hal+json");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.registerObjectMappersForType(SimpleBean.class, map -> map.put(halMediaType, objectMapper));
this.servletRequest.addHeader("Accept", halFormsMediaType + "," + halMediaType);
SimpleBean simpleBean = new SimpleBean();
simpleBean.setId(12L);
simpleBean.setName("Jason");
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(converter));
MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("getSimpleBean"), -1);
processor.writeWithMessageConverters(simpleBean, returnType, this.request);
assertThat(this.servletResponse.getHeader("Content-Type")).isEqualTo(halMediaType.toString());
assertThat(this.servletResponse.getContentAsString()).isEqualTo("{" + NEWLINE_SYSTEM_PROPERTY + " \"id\" : 12," + NEWLINE_SYSTEM_PROPERTY + " \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}");
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 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);
assertThat(servletResponse.getHeader("Content-Type")).isEqualTo(MediaType.APPLICATION_JSON_VALUE);
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveParameterizedWithTypeVariableArgument.
// SPR-14470
@Test
public void resolveParameterizedWithTypeVariableArgument() throws Exception {
Method method = MyParameterizedControllerWithList.class.getMethod("handleDto", List.class);
HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedControllerWithList(), method);
MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
@SuppressWarnings("unchecked") List<SimpleBean> result = (List<SimpleBean>) processor.resolveArgument(methodParam, container, request, factory);
assertThat(result).isNotNull();
assertThat(result.get(0).getName()).isEqualTo("Jad");
assertThat(result.get(1).getName()).isEqualTo("Robert");
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class ResponseBodyEmitterReturnValueHandlerTests method setup.
@BeforeEach
public void setup() throws Exception {
List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter());
this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, this.response);
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method produces.
@PathPatternsParameterizedTest
void produces(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ProducesController.class, usePathPatterns, wac -> {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
converters.add(new Jaxb2RootElementHttpMessageConverter());
RootBeanDefinition beanDef;
beanDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
beanDef.getPropertyValues().add("messageConverters", converters);
wac.registerBeanDefinition("handlerAdapter", beanDef);
beanDef = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
beanDef.getPropertyValues().add("messageConverters", converters);
wac.registerBeanDefinition("requestMappingResolver", beanDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("html");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml, text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html;q=0.9, application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/msword");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(406);
// SPR-16318
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/csv,application/problem+json");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(500);
assertThat(response.getContentType()).isEqualTo("application/problem+json");
assertThat(response.getContentAsString()).isEqualTo("{\"reason\":\"error\"}");
}
Aggregations