紧接上一篇文章展开主题,上篇链接:/qq_42227281/article/details/106869400
本篇:Request 获取Post请求 body的参数
1、Springboot配置过滤器
需要注意的是,我用的是springboot,自定义的filter 需要扫描,或者加注解才能生效,我这里加了注解
@WebFilter(filterName = "requestFilter", urlPatterns = "/*")
启动类开启@ServletComponentScan,@WebFilter才能生效
@ServletComponentScan
@SpringBootApplication//springboot注解
public class ConformityApplication {
public static void main(String[] args) {
(, args);
}
}
如果是基于xml配置的框架,就直接在xml中开启就可以了
package ;
import .*;
import ;
import ;
import ;
/**
* @Author: 王文龙
* @Date: 2020/6/89:33
* @Version: 1.0
* @Describe: 描述:
*/
@WebFilter(filterName = "requestFilter", urlPatterns = "/*")
public class RequestFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request1 = (HttpServletRequest) request;
if((("Content-Type")) || ("Content-Type").contains("multipart/form-data;")){
(request,response);
}else {
(new InputStreamReadRepeatableRequestWrapper(request1), response);
}
}
@Override
public void destroy() {
}
}
2、由于inputStream只能被读取一次,这时需要将流中数据存储起来,以便后续使用,继承HttpServletRequestWrapper
package ;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @Author: 王文龙
* @Date: 2020/6/2211:17
* @Version: 1.0
* @Describe: 描述:
*/
public class RequestWrapper extends HttpServletRequestWrapper {
private Logger logger = ();
private String encoding = "UTF-8";
private byte[] requestBodyIniBytes;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
ServletInputStream stream = ();
String requestBody = (stream, (encoding));
requestBodyIniBytes = (encoding);
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream in;
in = new ByteArrayInputStream(requestBodyIniBytes);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
@Override
public int read() throws IOException {
return ();
}
};
}
}
3、获取流中数据
package ;
import ;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 获取流中数据
* @Author: 王文龙
* @Date: 2020/6/2211:10
* @Version: 1.0
* @Describe: 描述:
*/
public class RequestHelper {
/**
* 获取流中的数据
*/
private static Logger logger = ();
public static Map getBodyString(ServletRequest request) {
StringBuilder sb = new StringBuilder();
InputStream inputStream = null;
BufferedReader reader = null;
try {
inputStream = ();
reader = new BufferedReader(new InputStreamReader(inputStream, ("UTF-8")));
String line = "";
while ((line = ()) != null) {
(line);
}
} catch (IOException e) {
("getBodyString出现问题!");
} finally {
if (inputStream != null) {
try {
();
} catch (IOException e) {
();
}
}
if (reader != null) {
try {
();
} catch (IOException e) {
();
}
}
}
return ((),);
}
}
4、在日志的工具类中直接调用RequestHelper 就可以拿到数据了
(request)获取处理后的body参数
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 保存日志的工具类
*
* @Author: 王文龙
* @Date: 2020/6/2011:39
* @Version: 1.0
* @Describe: 描述:
*/
public class SysLogUtil {
private static SysLogDao sysLogDao = ().getBean();
/**
* 保存日志
*/
public static void saveLog(HttpServletRequest request, Exception ex, String title) throws Exception {
SysLog log = new SysLog();
(title);
(ex == null ? "1" : "2");
//登陆人获取这块还没法实现,这个框架是我私下整合其他插件用框架,后面把Shiro或者SpringSecruity
("王文龙");
((request));
(("user-agent"));
(());
(((request)));
(());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = (new Date());
(format);
// 保存日志
boolean insert = (log);
(insert,"添加系统日志失败");
}
}