!!!更推荐切面做这些操作,
切面打印日志
get请求参数很好取出,但是对于post请求body里边的参数,因为是从流里边取,而这个流又只能取出来一次,如果你再拦截器里取出打印,后边的controller接口就无法在接受到请求参数,因此,你只能取出数据的同时,复制一份再放回去。
具体代码如下:
public class NewHttpRequestWrapper extends HttpServletRequestWrapper {
private final String body;
public NewHttpRequestWrapper(HttpServletRequest request) {
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try {
inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody() {
//为了去除空格、换行符等
return StringUtils.trimAllWhitespace(this.body);
}
}
拦截器获取请求body参数:
NewHttpRequestWrapper requestWrapper = new NewHttpRequestWrapper(request);
String body = requestWrapper.getBody();
通过filter将参数放回去:
@WebFilter(urlPatterns = "/*",filterName = "channelFilter")
public class RequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(servletRequest instanceof HttpServletRequest) {
requestWrapper = new NewHttpRequestWrapper((HttpServletRequest) servletRequest);
}
if(requestWrapper == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
filterChain.doFilter(requestWrapper, servletResponse);
}
}
@Override
public void destroy() {
}
}
同时要在启动类上声明filter启用,加上下边的注解
@ServletComponentScan
这样就可以了。
!!!!注意事项:复制流这个操作仅限于body数据格式是JSON的情况。如果是form-urllencoded这种key-value格式的,不能走复制流,否则controller会拿不到参数,这种key-value直接在拦截器里边request.getParameter就可以了,不用复制~
如果既有form表单数据,又有key-value参数,取的时候就会麻烦,看到了一篇博客,但是没这个需求没有做,而且也没看太懂怎么做。。
提取form表单数据
其他参考资料
版权声明:本文为weixin_45614626原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。