背景
http请求的返回值中添加或者解析特异的参数
示例
正常的请求返回值
{
"code":"userCode",
"name":"userName"
}
添加特异参数的返回值
{
"code":"userCode",
"name":"userName",
"sex":"男",
"age":"18",
}
如上json返回值,在返回报文中添加特异的返回参数
/**
* @description: 返回报文拦截器。支撑车型参数和配置动态话返回使用
* @author: kk.xie
* @date: 2021-01-27 15:16
**/
@WebFilter(filterName = "parameterWrapperFilter", urlPatterns = {"/test1/*", "/test2/*"})
@Order(100)
public class ParameterWrapperFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//定义一个新的request(名称是wrapper)
HttpServletRequest requestWrapper = new ParameterRequestWrapper(request);
ParameterResponseWrapper responseWrapper = new ParameterResponseWrapper(response);
PrintWriter out = response.getWriter();
//将修改过的request(wrapper)返回
chain.doFilter(requestWrapper, responseWrapper);
//对返回参数进行处理
out.print(new String(responseWrapper.getResponseData(request.getParameter("callback"))));
out.flush();
out.close();
}
}
/**
* @description: 返回报文拦截器
* @author: kk.xie
* @date: 2021-01-27 15:23
**/
public class ParameterResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
private PrintWriter writer;
public ParameterResponseWrapper(HttpServletResponse resp) throws IOException {
super(resp);
buffer = new ByteArrayOutputStream();
out = new WapperedOutputStream(buffer);
writer = new PrintWriter(new OutputStreamWriter(buffer,
this.getCharacterEncoding()));
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
@Override
public PrintWriter getWriter() throws UnsupportedEncodingException {
return writer;
}
@Override
public void flushBuffer() throws IOException {
if (out != null) {
out.flush();
}
if (writer != null) {
writer.flush();
}
}
@Override
public void reset() {
buffer.reset();
}
public byte[] getResponseData(String callback) throws IOException {
flushBuffer();
Pattern pattern = Pattern.compile(callback + "\\(" + "(.*)" + "\\);");
byte[] bytes = buffer.toByteArray();
String data = new String(bytes);
Matcher matcher = pattern.matcher(data);
if(matcher.find()){
String json = matcher.group(1);
JSONObject jsonObject = JSONObject.parseObject(json);
JSONObject dataJsonObject = jsonObject.getObject("data", JSONObject.class);
String otherParam = dataJsonObject.getString(Constant.OTHER_PARAM);
if(StringUtils.isNotEmpty(otherParam)){
JSONObject otherParamJsonObject = JSONObject.parseObject(otherParam);
otherParamJsonObject.forEach((k, v) -> {
if(!dataJsonObject.containsKey(k)){
dataJsonObject.put(k, v);
}
});
dataJsonObject.remove(Constant.OTHER_PARAM);
}
data = callback + "(" + jsonObject.toJSONString() + ");";
return data.getBytes();
}
return buffer.toByteArray();
}
private class WapperedOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos = null;
public WapperedOutputStream(ByteArrayOutputStream stream)
throws IOException {
bos = stream;
}
@Override
public void write(int b) throws IOException {
bos.write(b);
}
@Override
public void write(byte[] b) throws IOException {
bos.write(b, 0, b.length);
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public boolean isReady() {
return false;
}
}
}