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

The following examples show how to use org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractRender.java    From Aooms with Apache License 2.0 6 votes vote down vote up
/**
 * springmvc提供的render方法
 *
 */
public void springMvcRender(Object returnValue){
    try{
        // modelAndViewContainer init
        ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer();
        ServletWebRequest webRequest = new ServletWebRequest(AoomsContext.getRequest(), AoomsContext.getResponse());

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

        // get RequestMappingHandlerAdapter Bean
        RequestMappingHandlerAdapter requestMappingHandlerAdapter = (RequestMappingHandlerAdapter)ac1.getBean("requestMappingHandlerAdapter");

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

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

    } catch (Exception ex){
        throw AoomsExceptions.create("springmvc render error",ex);
    }
}
 
Example #2
Source File: WebapiApplicationRunListener.java    From summerframework with Apache License 2.0 5 votes vote down vote up
/**
 * 反射获取HandlerMethodReturnValueHandler集合
 */
private List<HandlerMethodReturnValueHandler>
    findHandlerMethodReturnValueHandlers(RequestMappingHandlerAdapter requestMappingHandlerAdapter)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Field returnValueHandlerCompositeField =
        ReflectionUtils.findField(RequestMappingHandlerAdapter.class, "returnValueHandlers");
    returnValueHandlerCompositeField.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(returnValueHandlerCompositeField,
        returnValueHandlerCompositeField.getModifiers() & ~Modifier.FINAL);
    HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite =
        (HandlerMethodReturnValueHandlerComposite)ReflectionUtils.getField(returnValueHandlerCompositeField,
            requestMappingHandlerAdapter);
    Field returnValueHandlersField =
        ReflectionUtils.findField(HandlerMethodReturnValueHandlerComposite.class, "returnValueHandlers");
    returnValueHandlersField.setAccessible(true);
    Field returnValueHandlerModifiersField = Field.class.getDeclaredField("modifiers");
    returnValueHandlerModifiersField.setAccessible(true);
    returnValueHandlerModifiersField.setInt(returnValueHandlersField,
        returnValueHandlersField.getModifiers() & ~Modifier.FINAL);
    List<HandlerMethodReturnValueHandler> returnValueHandlers =
        (List<HandlerMethodReturnValueHandler>)ReflectionUtils.getField(returnValueHandlersField,
            returnValueHandlerComposite);
    return returnValueHandlers;

}
 
Example #3
Source File: RequestMappingHandlerAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #4
Source File: ExceptionHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example #5
Source File: ExceptionHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #6
Source File: RequestMappingHandlerAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #7
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example #8
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #9
Source File: ExceptionHandlerExceptionResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #10
Source File: RequestMappingHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #11
Source File: ExceptionHandlerExceptionResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example #12
Source File: ExceptionHandlerExceptionResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #13
Source File: SpringMvcPolyfill.java    From Milkomeda with MIT License 5 votes vote down vote up
/**
 * 动态添加消息体响应切面
 * @param returnValueHandlers       响应处理器列表(需要注入 {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#getReturnValueHandlers()})
 * @param handlerExceptionResolver  异常处理解析器(需要注入 {@link org.springframework.web.servlet.HandlerExceptionResolver})
 * @param responseBodyAdvice        ResponseBodyAdvice
 */
public static void addDynamicResponseBodyAdvice(List<HandlerMethodReturnValueHandler> returnValueHandlers, HandlerExceptionResolver handlerExceptionResolver, ResponseBodyAdvice<?> responseBodyAdvice) {
    if (CollectionUtils.isEmpty(returnValueHandlers)) {
        return;
    }
    // 下面这行添加不起作用,由于内部构建已经完成
    // adapter.setResponseBodyAdvice(Collections.singletonList(responseBodyAdvice));
    for (HandlerMethodReturnValueHandler returnValueHandler : returnValueHandlers) {
        // 只有AbstractMessageConverterMethodArgumentResolver继承类型(主要是HttpEntityMethodProcessor、RequestResponseBodyMethodProcessor)有Advice Chain
        if (returnValueHandler instanceof AbstractMessageConverterMethodArgumentResolver) {
            // TODO <mark> 由于使用底层API, 这个advice.responseBodyAdvice属性在未来版本中可能会改
            List<Object> advices = ReflectUtil.invokeFieldPath(returnValueHandler, "advice.responseBodyAdvice");
            if (CollectionUtils.isEmpty(advices)) {
                continue;
            }
            advices.add(responseBodyAdvice);
        }
    }

    // 动态添加到异常处理(因为源码流程中的异常处理是新加载的HandlerExceptionResolver,与正常响应处理不是同个处理集)
    if (handlerExceptionResolver instanceof HandlerExceptionResolverComposite) {
        // SpringMVC默认为注册HandlerExceptionResolverComposite的Bean
        List<HandlerExceptionResolver> exceptionResolvers = ((HandlerExceptionResolverComposite) handlerExceptionResolver).getExceptionResolvers();
        if (CollectionUtils.isEmpty(exceptionResolvers)) {
            return;
        }
        for (HandlerExceptionResolver exceptionResolver : exceptionResolvers) {
            if (exceptionResolver instanceof ExceptionHandlerExceptionResolver) {
                HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite = ((ExceptionHandlerExceptionResolver) exceptionResolver).getReturnValueHandlers();
                if (returnValueHandlerComposite == null) {
                    return;
                }
                SpringMvcPolyfill.addDynamicResponseBodyAdvice(returnValueHandlerComposite.getHandlers(), null, responseBodyAdvice);
            }
        }
    }
}
 
Example #14
Source File: RequestMappingHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Configure the complete list of supported return value types thus
 * overriding handlers that would otherwise be configured by default.
 */
public void setReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> returnValueHandlers) {
	if (returnValueHandlers == null) {
		this.returnValueHandlers = null;
	}
	else {
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
		this.returnValueHandlers.addHandlers(returnValueHandlers);
	}
}
 
Example #15
Source File: ExceptionHandlerExceptionResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example #16
Source File: ExceptionHandlerExceptionResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the configured handlers, or possibly {@code null} if not
 * initialized yet via {@link #afterPropertiesSet()}.
 */
@Nullable
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers() {
	return this.returnValueHandlers;
}
 
Example #17
Source File: ExceptionHandlerExceptionResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the configured handlers, or possibly {@code null} if not
 * initialized yet via {@link #afterPropertiesSet()}.
 */
@Nullable
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers() {
	return this.returnValueHandlers;
}
 
Example #18
Source File: ServletInvocableHandlerMethod.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Register {@link HandlerMethodReturnValueHandler} instances to use to
 * handle return values.
 */
public void setHandlerMethodReturnValueHandlers(HandlerMethodReturnValueHandlerComposite returnValueHandlers) {
	this.returnValueHandlers = returnValueHandlers;
}
 
Example #19
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return the configured handlers, or possibly {@code null} if not
 * initialized yet via {@link #afterPropertiesSet()}.
 */
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers() {
	return this.returnValueHandlers;
}
 
Example #20
Source File: ServletInvocableHandlerMethod.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Register {@link HandlerMethodReturnValueHandler} instances to use to
 * handle return values.
 */
public void setHandlerMethodReturnValueHandlers(HandlerMethodReturnValueHandlerComposite returnValueHandlers) {
	this.returnValueHandlers = returnValueHandlers;
}
 
Example #21
Source File: ServletInvocableHandlerMethod.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Register {@link HandlerMethodReturnValueHandler} instances to use to
 * handle return values.
 */
public void setHandlerMethodReturnValueHandlers(HandlerMethodReturnValueHandlerComposite returnValueHandlers) {
	this.returnValueHandlers = returnValueHandlers;
}
 
Example #22
Source File: ExceptionHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Return the configured handlers, or possibly {@code null} if not
 * initialized yet via {@link #afterPropertiesSet()}.
 */
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers() {
	return this.returnValueHandlers;
}
 
Example #23
Source File: ServletInvocableHandlerMethod.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Register {@link HandlerMethodReturnValueHandler} instances to use to
 * handle return values.
 */
public void setHandlerMethodReturnValueHandlers(HandlerMethodReturnValueHandlerComposite returnValueHandlers) {
	this.returnValueHandlers = returnValueHandlers;
}