项目中遇到一类请求,所有的请求都会将请求参数放在header头中,每个接口都需要单独处理,因此考虑做一层封装,统一进行转换。
基本思路是增加一个拦截器,由拦截器对请求头进行解析,并转换为参数
首先集成一个HttpServletRequestWrapper,使得当前请求持有所有的参数表
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> params = new HashMap<>();
/**
* Constructs a request object wrapping the given request.
*
* @param request
* @throws IllegalArgumentException if the request is null
*/
public ParameterRequestWrapper(HttpServletRequest request) {
super(request);
//将参数表,赋予给当前的Map以便于持有request中的参数
this.params.putAll(request.getParameterMap());
}
/**
* 重载构造方法
*/
public ParameterRequestWrapper(HttpServletRequest request, Map<String, Object> extendParams) {
this(request);
//这里将扩展参数写入参数表
addAllParameters(extendParams);
}
/**
* 在获取所有的参数名,必须重写此方法,否则对象中参数值映射不上
*
* @return
*/
@Override
public Enumeration<String> getParameterNames() {
return new Vector(params.keySet()).elements();
}
/**
* 重写getParameter方法
*
* @param name 参数名
* @return 返回参数值
*/
@Override
public String getParameter(String name) {
String[] values = params.get(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
@Override
public String[] getParameterValues(String name) {
String[] values = params.get(name);
if (values == null || values.length == 0) {
return null;
}
return values;
}
/**
* 增加多个参数
*
* @param otherParams 增加的多个参数
*/
public void addAllParameters(Map<String, Object> otherParams) {
for (Map.Entry<String, Object> entry : otherParams.entrySet()) {
addParameter(entry.getKey(), entry.getValue());
}
}
/**
* 增加参数
*
* @param name 参数名
* @param value 参数值
*/
public void addParameter(String name, Object value) {
if (value != null) {
if (value instanceof String[]) {
params.put(name, (String[]) value);
} else if (value instanceof String) {
params.put(name, new String[]{(String) value});
} else {
params.put(name, new String[]{String.valueOf(value)});
}
}
}
}
复制代码
其次,集成一个Filter
import app.config.ParameterRequestWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class ParamFilter extends OncePerRequestFilter {
private final SiteUidRelatedUtils uidRelatedUtils;
@Autowired
public ParamFilter(SiteUidRelatedUtils uidRelatedUtils) {
this.uidRelatedUtils = uidRelatedUtils;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
Map<String,Object> paramter = new HashMap<>(16);
//这里获取请求头信息,并填入参数表
String token = request.getHeader(TOKEN);
paramter.put("token", token);
//其它参数......
ParameterRequestWrapper wrapper = new ParameterRequestWrapper(request, paramter);
filterChain.doFilter(wrapper, response);
return;
}
filterChain.doFilter(request, response);
}
复制代码
至此,将请求头的内容转换成参数,就可以通过实体类进行接收了。