Java Code Examples for org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#setReturnValueHandlers()

The following examples show how to use org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#setReturnValueHandlers() . 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: RaptorHandlerAdapterPostProcessor.java    From raptor with Apache License 2.0 6 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (RequestMappingHandlerAdapter.class.isAssignableFrom(ClassUtils.getUserClass(bean))) {
        RequestMappingHandlerAdapter adapter = (RequestMappingHandlerAdapter) bean;
        List<Object> advices = getRequestResponseBodyAdviceByReflect(adapter);
        RaptorHandlerMethodProcessor raptorHandlerMethodProcessor = new RaptorHandlerMethodProcessor(raptorMessageConverter,advices);

        ArrayList<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>(adapter.getArgumentResolvers());
        argumentResolvers.add(0, raptorHandlerMethodProcessor);
        adapter.setArgumentResolvers(argumentResolvers);

        ArrayList<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>(adapter.getReturnValueHandlers());
        returnValueHandlers.add(0, raptorHandlerMethodProcessor);
        adapter.setReturnValueHandlers(returnValueHandlers);
    }
    return bean;
}
 
Example 2
Source File: WxMvcConfiguration.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    RequestMappingHandlerAdapter requestMappingHandlerAdapter = this.beanFactory.getBean(RequestMappingHandlerAdapter.class);
    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
    List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
    if (beanFactory instanceof ConfigurableBeanFactory) {
        argumentResolvers.add(new WxArgumentResolver((ConfigurableBeanFactory) beanFactory));
    } else {
        argumentResolvers.add(new WxArgumentResolver(beanFactory.getBean(WxUserManager.class), beanFactory.getBean(WxUserProvider.class)));
    }
    // 可以换成@Autowired,Spring内部框架就是这样做的
    // returnValueHandlers.add(beanFactory.getBean(WxAsyncMessageReturnValueHandler.class));
    // returnValueHandlers.add(beanFactory.getBean(WxSyncMessageReturnValueHandler.class));
    returnValueHandlers.add(beanFactory.getBean(WxMappingReturnValueHandler.class));
    returnValueHandlers.add(beanFactory.getBean(WxMessageReturnValueHandler.class));
    argumentResolvers.addAll(requestMappingHandlerAdapter.getArgumentResolvers());
    returnValueHandlers.addAll(requestMappingHandlerAdapter.getReturnValueHandlers());
    requestMappingHandlerAdapter.setArgumentResolvers(argumentResolvers);
    requestMappingHandlerAdapter.setReturnValueHandlers(returnValueHandlers);
}
 
Example 3
Source File: WebMvcConfig.java    From example-restful-project with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
        RequestMappingHandlerAdapter adapter = applicationContext.getBean(RequestMappingHandlerAdapter.class);

        TeapotMappingHandler handler = new TeapotMappingHandler(
                mapper, getMessageConverters());

        List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
        argumentResolvers.add(handler);
        argumentResolvers.addAll(adapter.getArgumentResolvers());

        adapter.setArgumentResolvers(argumentResolvers);

        List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>();
        handlers.add(handler);  // must be first
        handlers.addAll(adapter.getReturnValueHandlers());

        adapter.setReturnValueHandlers(handlers);
    }
}
 
Example 4
Source File: ExcelDrivenConfiguration.java    From tools with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    RequestMappingHandlerAdapter handlerAdapter = this.applicationContext.getBean(RequestMappingHandlerAdapter.class);
    List<HandlerMethodReturnValueHandler> returnValueHandlers = handlerAdapter.getReturnValueHandlers();
    List<HandlerMethodReturnValueHandler> valueHandlers = new ArrayList<>();
    valueHandlers.add(new ExcelDrivenWriteHandler());
    valueHandlers.add(new ExcelDrivenReadHandler());
    if (returnValueHandlers != null) {
        valueHandlers.addAll(returnValueHandlers);
    }
    handlerAdapter.setReturnValueHandlers(valueHandlers);
}
 
Example 5
Source File: GuavaLFReturnValueHandler.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GuavaLFReturnValueHandler install(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
    final List<HandlerMethodReturnValueHandler> originalHandlers = new ArrayList<>(
            requestMappingHandlerAdapter.getReturnValueHandlers());

    final int deferredPos = indexOfType(originalHandlers, DeferredResultMethodReturnValueHandler.class);
    // Add our handler directly after the deferred handler.
    originalHandlers.add(deferredPos + 1, this);

    requestMappingHandlerAdapter.setReturnValueHandlers(originalHandlers);

    return this;
}
 
Example 6
Source File: SpringMvcConfiguration.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Bean
public List<HandlerMethodReturnValueHandler> returnValueHandlers(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
    final List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(requestMappingHandlerAdapter.getReturnValueHandlers());
    handlers.add(0, new ObserableReturnValueHandler());
    handlers.add(0, new ListenableFutureReturnValueHandler());
    requestMappingHandlerAdapter.setReturnValueHandlers(handlers);
    return handlers;
}