上次在每个接口的方法体内,写了log.info。觉得很麻烦,于是搞了一个接口入参拦截器。统一打印
打印出参,闲下来会补充。
一、过滤器
@Slf4j
@Configuration
public class ServletFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletWrapper requestWrapper = new ServletWrapper((HttpServletRequest) request);
log.info("request-json : {}",requestWrapper.getBodyContent());
chain.doFilter(requestWrapper, response);
}
/**
* servlet适配器
*/
static class ServletWrapper extends BaseHttpServletRequestWrapper{
public ServletWrapper(HttpServletRequest request) {
super(request);
}
}
}
二、请求适配器
默认的servlet的body貌似只支持读一次,下面的适配器支持多次读取body字符。
public abstract class BaseHttpServletRequestWrapper extends HttpServletRequestWrapper {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseHttpServletRequestWrapper.class);
protected static Charset charset = Charset.forName("UTF-8");
protected String bodyContent;
protected HttpServletRequest request;
public BaseHttpServletRequestWrapper(HttpServletRequest request) {
this(request, true);
}
public BaseHttpServletRequestWrapper(HttpServletRequest request, boolean init) {
super(request);
this.request = request;
if (init) {
this.initWrapper();
}
}
protected HttpServletRequestWrapper initWrapper() {
if (this.checkIsWrapperRequest()) {
BaseHttpServletRequestWrapper requestWrapper = (BaseHttpServletRequestWrapper)this.request;
this.bodyContent = requestWrapper.bodyContent;
} else {
this.readBodyBytes();
}
return this;
}
public String getBodyContent() {
this.readBodyBytes();
return this.bodyContent;
}
protected void readBodyBytes() {
if (this.bodyContent == null) {
try {
byte[] bytes = this.readInputBody(this.request.getInputStream());
this.bodyContent = new String(bytes, charset);
} catch (IOException var2) {
LOGGER.error("读取输入流失败", var2);
throw new RuntimeException(var2);
}
}
}
protected byte[] readInputBody(InputStream inputStream) throws IOException {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] var5;
try {
byte[] buffer = new byte[1024];
while(true) {
int len;
if ((len = inputStream.read(buffer)) <= -1) {
byteArrayOutputStream.flush();
var5 = byteArrayOutputStream.toByteArray();
break;
}
byteArrayOutputStream.write(buffer, 0, len);
}
} catch (Throwable var7) {
try {
byteArrayOutputStream.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
throw var7;
}
byteArrayOutputStream.close();
return var5;
} catch (Exception var8) {
throw new IOException(var8);
}
}
public BufferedReader getReader() throws IOException {
return this.checkIsWrapperRequest() ? this.request.getReader() : new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public ServletInputStream getInputStream() throws IOException {
return (ServletInputStream)(this.checkIsWrapperRequest() ? this.request.getInputStream() : new WrappedServletInputStream(this.getRequestInputStream()));
}
protected InputStream getRequestInputStream() {
return new ByteArrayInputStream(this.bodyContent.getBytes(charset));
}
protected boolean checkIsWrapperRequest() {
return this.request instanceof BaseHttpServletRequestWrapper;
}
public static class WrappedServletInputStream extends ServletInputStream {
private InputStream inputStream;
public WrappedServletInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public boolean isFinished() {
return false;
}
public boolean isReady() {
return true;
}
public void setReadListener(ReadListener readListener) {
}
public int read() throws IOException {
return this.inputStream.read();
}
public int read(byte[] buffer) throws IOException {
return this.inputStream.read(buffer);
}
}
}