class NewRequest:
import lombok.SneakyThrows;
import lombok.var;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
public class NewRequest extends HttpServletRequestWrapper {
byte[] body;
@SneakyThrows
public NewRequest(HttpServletRequest request) {
super(request);
body = BinaryUtil.getBytes(request.getInputStream());
}
@Override
public ServletInputStream getInputStream() {
var input = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() {
return input.read();
}
@Override
public boolean isFinished() {
return input.available() == 0;
}
@Override
public boolean isReady() {
return input.available() == 1;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
}
class RequestFilter:
import org.springframework.context.annotation.Configuration;
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;
@Configuration
public class RequestFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
if (StringUtil.contains(request.getContentType(), "application/json"))
request = new NewRequest(request);//异常日志需要, json要支持重复获取
chain.doFilter(request, response);
}
}
放任意位置即可, 只替换了contentType包含application/json的请求