Search in sources :

Example 1 with HandlerMethodReturnValueHandler

use of org.springframework.web.method.support.HandlerMethodReturnValueHandler in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolver method getDefaultReturnValueHandlers.

/**
	 * Return the list of return value handlers to use including built-in and
	 * custom handlers provided via {@link #setReturnValueHandlers}.
	 */
protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
    List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>();
    // Single-purpose return value types
    handlers.add(new ModelAndViewMethodReturnValueHandler());
    handlers.add(new ModelMethodProcessor());
    handlers.add(new ViewMethodReturnValueHandler());
    handlers.add(new HttpEntityMethodProcessor(getMessageConverters(), this.contentNegotiationManager, this.responseBodyAdvice));
    // Annotation-based return value types
    handlers.add(new ModelAttributeMethodProcessor(false));
    handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.contentNegotiationManager, this.responseBodyAdvice));
    // Multi-purpose return value types
    handlers.add(new ViewNameMethodReturnValueHandler());
    handlers.add(new MapMethodProcessor());
    // Custom return value types
    if (getCustomReturnValueHandlers() != null) {
        handlers.addAll(getCustomReturnValueHandlers());
    }
    // Catch-all
    handlers.add(new ModelAttributeMethodProcessor(true));
    return handlers;
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) ModelMethodProcessor(org.springframework.web.method.annotation.ModelMethodProcessor) MapMethodProcessor(org.springframework.web.method.annotation.MapMethodProcessor) ArrayList(java.util.ArrayList) ModelAttributeMethodProcessor(org.springframework.web.method.annotation.ModelAttributeMethodProcessor)

Example 2 with HandlerMethodReturnValueHandler

use of org.springframework.web.method.support.HandlerMethodReturnValueHandler in project spring-framework by spring-projects.

the class TestPathHelper method testReturnValueHandlers.

private void testReturnValueHandlers(Object bean) {
    assertNotNull(bean);
    Object value = new DirectFieldAccessor(bean).getPropertyValue("customReturnValueHandlers");
    assertNotNull(value);
    assertTrue(value instanceof List);
    @SuppressWarnings("unchecked") List<HandlerMethodReturnValueHandler> handlers = (List<HandlerMethodReturnValueHandler>) value;
    assertEquals(2, handlers.size());
    assertEquals(TestHandlerMethodReturnValueHandler.class, handlers.get(0).getClass());
    assertEquals(TestHandlerMethodReturnValueHandler.class, handlers.get(1).getClass());
    assertNotSame(handlers.get(0), handlers.get(1));
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) List(java.util.List)

Example 3 with HandlerMethodReturnValueHandler

use of org.springframework.web.method.support.HandlerMethodReturnValueHandler in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolverTests method setCustomReturnValueHandlers.

@Test
public void setCustomReturnValueHandlers() {
    HandlerMethodReturnValueHandler handler = new ViewNameMethodReturnValueHandler();
    this.resolver.setCustomReturnValueHandlers(Collections.singletonList(handler));
    this.resolver.afterPropertiesSet();
    assertTrue(this.resolver.getReturnValueHandlers().getHandlers().contains(handler));
    assertMethodProcessorCount(RESOLVER_COUNT, HANDLER_COUNT + 1);
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) Test(org.junit.Test)

Example 4 with HandlerMethodReturnValueHandler

use of org.springframework.web.method.support.HandlerMethodReturnValueHandler in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterTests method setAlwaysUseRedirectAttributes.

@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
    HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
    HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
    HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();
    this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
    this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
    this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
    this.handlerAdapter.afterPropertiesSet();
    this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
    ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);
    assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) FlashMap(org.springframework.web.servlet.FlashMap) ModelMethodProcessor(org.springframework.web.method.annotation.ModelMethodProcessor) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 5 with HandlerMethodReturnValueHandler

use of org.springframework.web.method.support.HandlerMethodReturnValueHandler in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterTests method setReturnValueHandlers.

@Test
public void setReturnValueHandlers() {
    HandlerMethodReturnValueHandler handler = new ModelMethodProcessor();
    this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(handler));
    this.handlerAdapter.afterPropertiesSet();
    assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1);
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) ModelMethodProcessor(org.springframework.web.method.annotation.ModelMethodProcessor) Test(org.junit.Test)

Aggregations

HandlerMethodReturnValueHandler (org.springframework.web.method.support.HandlerMethodReturnValueHandler)10 Test (org.junit.Test)6 ModelMethodProcessor (org.springframework.web.method.annotation.ModelMethodProcessor)5 ArrayList (java.util.ArrayList)3 List (java.util.List)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 MapMethodProcessor (org.springframework.web.method.annotation.MapMethodProcessor)2 ModelAttributeMethodProcessor (org.springframework.web.method.annotation.ModelAttributeMethodProcessor)2 HandlerMethodArgumentResolver (org.springframework.web.method.support.HandlerMethodArgumentResolver)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ListenableFutureReturnValueHandler (com.kixeye.chassis.transport.http.ListenableFutureReturnValueHandler)1 ObserableReturnValueHandler (com.kixeye.chassis.transport.http.ObserableReturnValueHandler)1 Bean (org.springframework.context.annotation.Bean)1 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 TestBean (org.springframework.tests.sample.beans.TestBean)1 CallableProcessingInterceptor (org.springframework.web.context.request.async.CallableProcessingInterceptor)1 DeferredResultProcessingInterceptor (org.springframework.web.context.request.async.DeferredResultProcessingInterceptor)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1