Java Code Examples for org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite

Following code examples demonstrate how to use org.springframework.web.method.support.HandlerMethodReturnValueHandlerCompositefrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.web.method.support.HandlerMethodReturnValueHandlerCompositeand various code implementation of this class.

    public void afterPropertiesSet() throws Exception {
        
    	HandlerMethodReturnValueHandlerComposite returnValueHandlers = adapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> handlers = Lists.newArrayList(returnValueHandlers.getHandlers());
        
    	decorateHandlers(handlers);
        adapter.setReturnValueHandlers(handlers);
    } 


    @Override
    protected HandlerMethodReturnValueHandlerComposite getReturnValueHandlers(
            Collection<MediaType> mediaTypes)
    {
        HandlerMethodReturnValueHandlerComposite handlers = new HandlerMethodReturnValueHandlerComposite();
        handlers.addHandler(FILE_SERVICE_METHOD_PROCESSOR);
        mediaTypes.add(MediaType.APPLICATION_JSON);
        handlers.addHandlers(super.getReturnValueHandlers(mediaTypes).getHandlers());
        return handlers;
    } 

    @Override
    protected HandlerMethodReturnValueHandlerComposite getReturnValueHandlers(
            Collection<MediaType> mediaTypes)
    {
        HandlerMethodReturnValueHandlerComposite handlers = new HandlerMethodReturnValueHandlerComposite();
        handlers.addHandler(STORAGE_OBJECT_METHOD_PROCESSOR);
        mediaTypes.add(MediaType.APPLICATION_JSON);
        handlers.addHandlers(super.getReturnValueHandlers(mediaTypes).getHandlers());
        return handlers;
    } 

    public void afterPropertiesSet() throws Exception {
        HandlerMethodReturnValueHandlerComposite returnValueHandlers = adapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> handlers = Lists.newArrayList(returnValueHandlers.getHandlers());

        ModelMethodReturnValueHandler modelHandler = null;

        for (HandlerMethodReturnValueHandler handler : handlers) {
            if (handler instanceof RequestResponseBodyMethodProcessor)
            {
                modelHandler = new ModelMethodReturnValueHandler(handler);
                break;
            }
        }

        handlers.add(0, modelHandler);
        adapter.setReturnValueHandlers(handlers);
    } 

	public void afterPropertiesSet() {
		super.afterPropertiesSet();
		Method getHandler = null;
		try {
			getHandler = org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class.getDeclaredMethod("getDefaultReturnValueHandlers");
			Field returnValueHandlers = org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class.getDeclaredField("returnValueHandlers");
			returnValueHandlers.setAccessible(true);
			getHandler.setAccessible(true);
			
			@SuppressWarnings("unchecked")
			List<HandlerMethodReturnValueHandler> handlers = (List<HandlerMethodReturnValueHandler>) getHandler.invoke(this);
			
			HandlerMethodReturnValueHandler responseBodyHandler = null;
			for(HandlerMethodReturnValueHandler obj : handlers) {
				if(obj instanceof RequestResponseBodyMethodProcessor) {
					responseBodyHandler = obj;
					break;
				}
			}
			
			handlers.add(0, new APIHandlerMethodReturnValueHandler(responseBodyHandler));
			
			returnValueHandlers.set(this, new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers));
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 

    public void springMvcRender(Object returnValue){
        try{
            ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer();
            ServletWebRequest webRequest = new ServletWebRequest(AoomsContext.getRequest(), AoomsContext.getResponse());

            ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(AoomsContext.getServletContext());

            RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter)ac1.getBean("requestMappingHandlerAdapter");

            HandlerMethodReturnValueHandlerComposite handlerMethodReturnValueHandlerComposite = new HandlerMethodReturnValueHandlerComposite();
            handlerMethodReturnValueHandlerComposite.addHandlers(requestMappingHandlerAdapter.getReturnValueHandlers());

            HandlerMethod method = AoomsContext.getMethodHandler();
            handlerMethodReturnValueHandlerComposite.handleReturnValue(returnValue, method.getReturnType(), modelAndViewContainer, webRequest);

        } catch (Exception ex){
            throw AoomsExceptions.create("springmvc render error",ex);
        }
    }; 

    private void modifyReturnHandlers() {
        Field field = ReflectionUtils.findField(this.getClass(), "returnValueHandlers");
        ReflectionUtils.makeAccessible(field);
        HandlerMethodReturnValueHandlerComposite rc = (HandlerMethodReturnValueHandlerComposite) ReflectionUtils.getField(field, this);
        field = ReflectionUtils.findField(rc.getClass(), "returnValueHandlers");
        ReflectionUtils.makeAccessible(field);
        List<HandlerMethodReturnValueHandler> resolvers = (List<HandlerMethodReturnValueHandler>) ReflectionUtils.getField(field, rc);

        resolvers.add(0, new JsonHandlerMethodReturnValueHandler());
    } 

Advertisement
Javadoc
Handles method return values by delegating to a list of registered HandlerMethodReturnValueHandler H

andlerMethodReturnValueHandlers. Previously resolved return types are cached for faster lookups. @author Rossen Stoyanchev @since 3.1

Read More
Advertisement