use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterTests method modelAttributePackageNameAdvice.
@Test
public void modelAttributePackageNameAdvice() throws Exception {
this.webAppContext.registerSingleton("mapa", ModelAttributePackageAdvice.class);
this.webAppContext.registerSingleton("manupa", ModelAttributeNotUsedPackageAdvice.class);
this.webAppContext.refresh();
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
this.handlerAdapter.afterPropertiesSet();
ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertEquals("lAttr1", mav.getModel().get("attr1"));
assertEquals("gAttr2", mav.getModel().get("attr2"));
assertEquals(null, mav.getModel().get("attr3"));
}
use of org.springframework.web.method.HandlerMethod 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.web.method.HandlerMethod 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.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndJsonMessageConverter.
@Test
public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() 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 MappingJackson2HttpMessageConverter());
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\""));
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 RequestMappingHandlerAdapterTests method testJsonp.
private void testJsonp(String value, boolean validValue) throws Exception {
this.request = new MockHttpServletRequest("GET", "/");
this.request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
this.request.setParameter("c", value);
this.response = new MockHttpServletResponse();
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleWithResponseEntity");
this.handlerAdapter.afterPropertiesSet();
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertEquals(200, this.response.getStatus());
if (validValue) {
assertEquals("/**/" + value + "({\"foo\":\"bar\"});", this.response.getContentAsString());
} else {
assertEquals("{\"foo\":\"bar\"}", this.response.getContentAsString());
}
}
Aggregations