use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method jacksonSubTypeList.
// SPR-13318
@Test
public void jacksonSubTypeList() throws Exception {
Method method = JacksonController.class.getMethod("handleSubTypeList");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
Object returnValue = new JacksonController().handleSubTypeList();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
String content = this.servletResponse.getContentAsString();
assertTrue(content.contains("\"id\":123"));
assertTrue(content.contains("\"name\":\"foo\""));
assertTrue(content.contains("\"id\":456"));
assertTrue(content.contains("\"name\":\"bar\""));
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method resolveArgumentWithJacksonJsonView.
// SPR-12501
@Test
public void resolveArgumentWithJacksonJsonView() throws Exception {
String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_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 MappingJackson2HttpMessageConverter());
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.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method defaultCharset.
// SPR-13631
@Test
public void defaultCharset() throws Exception {
Method method = JacksonController.class.getMethod("defaultCharset");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
Object returnValue = new JacksonController().defaultCharset();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndJsonMessageConverter.
@Test
public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception {
Method method = JacksonController.class.getMethod("handleResponseEntity");
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, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
Object returnValue = new JacksonController().handleResponseEntity();
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\""));
assertFalse(content.contains("\"withoutView\":\"without\""));
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterIntegrationTests method handleHttpEntity.
@Test
public void handleHttpEntity() throws Exception {
Class<?>[] parameterTypes = new Class<?>[] { HttpEntity.class };
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.setContent("Hello Server".getBytes("UTF-8"));
HandlerMethod handlerMethod = handlerMethod("handleHttpEntity", parameterTypes);
ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
assertNull(mav);
assertEquals(HttpStatus.ACCEPTED.value(), response.getStatus());
assertEquals("Handled requestBody=[Hello Server]", new String(response.getContentAsByteArray(), "UTF-8"));
assertEquals("headerValue", response.getHeader("header"));
// set because of @SesstionAttributes
assertEquals("no-store", response.getHeader("Cache-Control"));
}
Aggregations