包装spring的Controller的ResponseBody返回结果

ResponseBody是通过RequestResponseBodyMethodProcessor起作用的。
我们的做法是写一个包装类,替换掉他


package com.qq.secure.rj.webapp;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.List;

/**
 * 替换为ResponseBody包装类
 * Created by yunbinhuang on 2015/10/13.
 */
public class ResponseBodyWrapFactoryBean implements InitializingBean {

    @Autowired
    private RequestMappingHandlerAdapter adapter;

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

    private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        for (HandlerMethodReturnValueHandler handler : handlers) {
            if (handler instanceof RequestResponseBodyMethodProcessor) {
                //用自己的ResponseBody包装类替换掉框架的,达到返回Result的效果
                ResponseBodyWrapHandler decorator = new ResponseBodyWrapHandler(handler);
                int index = handlers.indexOf(handler);
                handlers.set(index, decorator);
                break;
            }
        }
    }

}



package com.qq.secure.rj.webapp;

import com.qq.secure.rj.core.Result;
import com.qq.secure.rj.util.JacksonUtil;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * ResponseBody包装类,返回Result
 * Created by yunbinhuang on 2015/10/13.
 */
public class ResponseBodyWrapHandler implements HandlerMethodReturnValueHandler{
    private final HandlerMethodReturnValueHandler delegate;
    public  ResponseBodyWrapHandler(HandlerMethodReturnValueHandler delegate){
      this.delegate=delegate;
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return delegate.supportsReturnType(returnType);
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        Result result = Result.success(returnValue);
        delegate.handleReturnValue(result,returnType,mavContainer,webRequest);
    }
}



 @Bean
    public ResponseBodyWrapFactoryBean getResponseBodyWrap() {
        return  new ResponseBodyWrapFactoryBean();
    }



注意  我们添加一个类似的HandlerMethodReturnValueHandler 是不起作用的。因为spring一种返回类型只能用一个。

你可能感兴趣的:(spring)