得到RequestVO

import java.io.IOException;
import java.nio.charset.Charset;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.log4j.Logger;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yundaex.common.basic.vo.RequestVO;
import com.yundaex.common.basic.vo.UserContext;
import com.yundaex.common.bidiboxing.constants.CommunicationConstants;
import com.yundaex.common.bidiboxing.convert.PayloadConvertException;
import com.yundaex.common.exception.WrappedIOException;
import com.yundaex.utility.json.YDJSONConvertException;
import com.yundaex.utility.protobuf.ProtobufUtils;
import com.yundaex.utility.xml.XMLUtils;


public class HttpHelper {
    
    //logger for class HttpHelper
    private static final Logger logger = 
            Logger.getLogger(HttpHelper.class);
    
    /**
     * generate requestVO from http servlet request
     * */
    public static RequestVO generateRequestVO(HttpServletRequest request) {
        RequestVO requestVO = null;
        // get request vo from request
        Object attribute = request.getAttribute("requestVO");
        if (attribute != null && RequestVO.class.isInstance(attribute)) {
            requestVO = (RequestVO) attribute;
        } else {
            String payloadFormat = getPayloadFormat(request);
            String voStr = request.getParameter("payload");
            if (null == voStr && !CommunicationConstants.DATA_DIALECT_PROTOBUF.equals(payloadFormat)) {
                try {
                    Charset charset = Charset.forName("UTF-8");
                    ServletInputStream inputStream = request.getInputStream();
                    voStr = StreamUtils.copyToString(inputStream, charset);
                } catch (IOException e) {
                    throw new YDJSONConvertException("payload convert exception,couldn't convert to bean");
                }
            }
            if (null != voStr) {
                if (logger.isDebugEnabled()) {
                    logger.debug(voStr);
                }
                if (StringUtils.isEmpty(voStr)) {
                    requestVO = new RequestVO();
                }
                if ( !StringUtils.isEmpty(voStr) && CommunicationConstants.DATA_DIALECT_JSON.equals(payloadFormat)) {
                    try{
                        Feature[] features = {Feature.SortFeidFastMatch};
                        requestVO =  JSONObject.parseObject(voStr, RequestVO.class, features);
                    } catch(Exception e) {
                        throw new PayloadConvertException("JSON数据不合法,无法解析。");
                    }
                    
                }
                if (!StringUtils.isEmpty(voStr) && CommunicationConstants.DATA_DIALECT_XML.equals(payloadFormat)) {
                    requestVO = XMLUtils.unmarshall(voStr, RequestVO.class);
                }
            }
            if (null == voStr && CommunicationConstants.DATA_DIALECT_PROTOBUF.equals(payloadFormat)) {
                ServletInputStream inputStream = null;
                try {
                    inputStream = request.getInputStream();
                } catch (IOException e) {
                    throw new WrappedIOException();
                }
                requestVO = ProtobufUtils.unmarshal(inputStream, RequestVO.class);
            }
            if (requestVO != null) {
                requestVO.setFormat(payloadFormat);
                if (null == requestVO.getUserContext()) {
                    requestVO.setUserContext(UserContext.constructEmptyUserContext());
                }
                requestVO.getUserContext().setDialect(payloadFormat);
                request.setAttribute("requestVO", requestVO);
            } else {
                throw new YDJSONConvertException("payload is blank,couldn't convert to bean");
            }
        }
        return requestVO;
    }
    
    private static String getPayloadFormat(HttpServletRequest request) {
        if (request.getContentType() != null) {
            // System.out.println(request.getContentType());
            if (request.getContentType().contains("application/json")) {
                return CommunicationConstants.DATA_DIALECT_JSON;
            } else if (request.getContentType().contains("application/xml")) {
                return CommunicationConstants.DATA_DIALECT_XML;
            } else if (request.getContentType().contains("application/x-protobuf")) {
                return CommunicationConstants.DATA_DIALECT_PROTOBUF;
            } else if (request.getContentType().contains("multipart/form-data")) {
                if(logger.isDebugEnabled()) {
                    logger.debug(request.getClass());
                }
                if(HttpServletRequestWrapper.class.isAssignableFrom(request.getClass())) {
                    HttpServletRequestWrapper httpServletRequestWrapper = (HttpServletRequestWrapper) request;
                    request = (HttpServletRequest) httpServletRequestWrapper.getRequest();
                }
                if (MultipartHttpServletRequest.class.isAssignableFrom(request.getClass())) {
                    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                    String format = multiRequest.getParameter("format");
                    if (format == null || format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_JSON)) {
                        return CommunicationConstants.DATA_DIALECT_JSON;
                    }
                    if (format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_XML)) {
                        return CommunicationConstants.DATA_DIALECT_XML;
                    }
                    if (format.equalsIgnoreCase(CommunicationConstants.DATA_DIALECT_PROTOBUF)) {
                        return CommunicationConstants.DATA_DIALECT_PROTOBUF;
                    }
                }
            }
        }
        return CommunicationConstants.DATA_DIALECT_JSON;
    }
}
import java.util.Date;

public class RequestVO {
    
    private Object data;
    
    private String validation;
    
    private String format;
    
    private String username;
    
    private Date dateTime;
    
    private UserContext userContext;
    
    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }
    
    public String getValidation() {
        return validation;
    }

    public void setValidation(String validation) {
        this.validation = validation;
    }
    
    /**
     * @return the userContext
     */
    public UserContext getUserContext() {
        return userContext;
    }
    
    /**
     * @param userContext the userContext to set
     */
    public void setUserContext(UserContext userContext) {
        this.userContext = userContext;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the data
     */
    public Object getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(Object data) {
        this.data = data;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }
    
}
public interface CommunicationConstants {
    
    public static final String DATA_DIALECT_JSON = "json";
    
    public static final String DATA_DIALECT_XML = "xml";
    
    public static final String DATA_DIALECT_PROTOBUF = "protobuf";
    
    
}
public class UserContext {
    
    //auto calculate
    private Long userId;
    
    //copy form request vo
    private String username;
    
    private String userProp;
    
    private Long domainId;
    
    //auto calculate
    private String userType;
    
    //auto calculate;from contentTypoe;never transform
    private String dialect;
    
    //optional ;support for multi-timezone 
    private Integer timeZone;
    
    //required;transform
    private Integer curOrganization;
    
    //optional;support gb_rbac;transform
    private Integer curGroup;
    
    //auto calculate. default is RBAC,optional.
    private String pmModel;
    
    private String msg;
    
    private String appKey;
    
    // API 接口名称
    private String method;
    
    //设备类型
    private String deviceType;
    
    /**
     * @return the dialect
     */
    public String getDialect() {
        return dialect;
    }

    /**
     * @param dialect the dialect to set
     */
    public void setDialect(String dialect) {
        this.dialect = dialect;
    }

    /**
     * @return the timeZone
     */
    public Integer getTimeZone() {
        return timeZone;
    }

    /**
     * @param timeZone the timeZone to set
     */
    public void setTimeZone(Integer timeZone) {
        this.timeZone = timeZone;
    }

    /**
     * @return the userId
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public static UserContext constructEmptyUserContext(){
        UserContext emptyContext = new UserContext();
        return emptyContext;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserProp() {
        return userProp;
    }

    public void setUserProp(String userProp) {
        this.userProp = userProp;
    }

    /**
     * @return the userType
     */
    public String getUserType() {
        return userType;
    }

    /**
     * @param userType the userType to set
     */
    public void setUserType(String userType) {
        this.userType = userType;
    }

    /**
     * @return the pmModel
     */
    public String getPmModel() {
        return pmModel;
    }

    /**
     * @param pmModel the pmModel to set
     */
    public void setPmModel(String pmModel) {
        this.pmModel = pmModel;
    }

    /**
     * @return the curGroup
     */
    public Integer getCurGroup() {
        return curGroup;
    }

    /**
     * @param curGroup the curGroup to set
     */
    public void setCurGroup(Integer curGroup) {
        this.curGroup = curGroup;
    }

    /**
     * @return the curOrganization
     */
    public Integer getCurOrganization() {
        return curOrganization;
    }

    /**
     * @param curOrganization the curOrganization to set
     */
    public void setCurOrganization(Integer curOrganization) {
        this.curOrganization = curOrganization;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the appKey
     */
    public String getAppKey() {
        return appKey;
    }

    /**
     * @param appKey the appKey to set
     */
    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    /**
     * @return the method
     */
    public String getMethod() {
        return method;
    }

    /**
     * @param method the method to set
     */
    public void setMethod(String method) {
        this.method = method;
    }

    /**
     * @return the domainId
     */
    public Long getDomainId() {
        return domainId;
    }

    /**
     * @param domainId the domainId to set
     */
    public void setDomainId(Long domainId) {
        this.domainId = domainId;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }
}
原文地址:https://www.cnblogs.com/tonggc1668/p/6543959.html