最近在写日志管理,想着使用拦截器加注解的方式,但是遇到了一个问题,就是如果使用@RequestBody注解接收的参数只能读取一次,造成了我在拦截器中如果接收了参数,在Controller层就接收不到了,为了解决这个问题,在网上查了方法。自定义一个MyRequestWrapper 继承 HttpServletRequestWrapper不多说,看代码:

package cn.huimin100.cms.handler;
 
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
 
/**
* 与HttpServletRequestWrapperFilter配合使用,保证@RequestBody注解的参数可以读取两次
* 因为要在拦截器中获取参数
*/
public class MyRequestWrapper extends HttpServletRequestWrapper {
private final String body;
public MyRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream 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) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
body = stringBuilder.toString();
}
 
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
 
 
@Override
public boolean isFinished() {
return false;
}
 
@Override
public boolean isReady() {
return false;
}
 
@Override
public void setReadListener(ReadListener readListener) {
 
}
 
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
 
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
 
public String getBody() {
return this.body;
}
}
 
需要结合过滤器使用:
package cn.huimin100.cms.handler;
 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
/**
* 该过滤器与MyRequestWrapper配合使用,可以让@RequestBody注解的参数至少可以读取两次,
* 从而使得在拦截器中可以获取@RequestBody注解的参数。
*/
@WebFilter(filterName="HttpServletRequestWrapperFilter",urlPatterns="/*")
public class HttpServletRequestWrapperFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
 
}
 
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper= null;
if(request instanceof HttpServletRequest){
requestWrapper = new MyRequestWrapper((HttpServletRequest) request);
}
if(null == requestWrapper){
filterChain.doFilter(request,servletResponse);
}else{
filterChain.doFilter(requestWrapper,servletResponse);
}
}
 
@Override
public void destroy() {
 
}
}
 
拦截器中这样获取参数
package cn.huimin100.cms.handler;
 
import cn.huimin100.cms.config.LogAnnotation;
import cn.huimin100.cms.mapper.newcms.ActionLogsMapper;
import cn.huimin100.cms.pojo.po.ActionLogs;
import cn.huimin100.cms.util.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
 
/**
* 在读取@RequestBody注解的参数的时候,需要配合MyRequestWrapper使用
* 一遍保证这些参数可以在Controller层再次读取
*/
public class LogManageInterceptor extends HandlerInterceptorAdapter {
private final Logger logger = LoggerFactory.getLogger(LogManageInterceptor.class);
 
@Autowired
private ActionLogsMapper logMapper;
 
public static final String USER_NAME="user__name";
public static final String BRANCH_ID="branch__id";
public static final String USER_ID="user__id";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
if(annotation != null){
try{
ActionLogs log = new ActionLogs();
//操作类型
log.setAction(annotation.action().getCode());
//操作目标方法
log.setCategory(annotation.targetMethod().getCode());
//操作人姓名
log.setActionTime(DateFormatUtils.format(new Date(), DateUtil.DATETIME_DEFAULT_FORMAT));
log.setIsmiddlemode(2);
Map<String, String[]> parameterMap = request.getParameterMap();
if(parameterMap == null || parameterMap.isEmpty()){
MyRequestWrapper myRequestWrapper = new MyRequestWrapper((HttpServletRequest) request);
String body = myRequestWrapper.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
log.setOperator(jsonObject.getString(USER_NAME));
log.setOperatorId(jsonObject.getInteger(USER_ID));
log.setBranchId(jsonObject.getInteger(BRANCH_ID));
}else{
log.setOperator(request.getParameter(USER_NAME));
log.setOperatorId(Integer.valueOf(request.getParameter(USER_ID)));
log.setBranchId(Integer.valueOf(request.getParameter(BRANCH_ID)));
}
logMapper.insertSelective(log);
}catch (Exception e){
logger.error("记录操作日志错误 e:{}",e);
}
}
}
return true;
}
}

@RequestBody注解的参数仅仅读取一次的问题解决。的更多相关文章

  1. spring中RequestBody注解接收参数时用JSONField转参数名无效问题

    问题: 在springboot项目中使用@RequestBody注解接收post请求中body里的json参数的情况.即: @RequestMapping(value = "/get-use ...

  2. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  3. Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...

  4. 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...

  5. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  6. (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  7. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  8. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  9. 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox   因为写的很好很全,所以转载过来 引言:接上一篇文章, ...

随机推荐

  1. sourceTree回退撤销commit

    https://blog.csdn.net/gang544043963/article/details/71511958

  2. pycharm import pygame 出现报错:No module named 'pygame'

    首先发现装的Python 有问题原来的Python3.6.4版本安装完成后Scripts文件夹里空白的,什么也没有,从https://www.python.org/downloads/windows/ ...

  3. Linux下Solr单机版、集群版安装与配置

    一.安装 1.需要的安装包有apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 这里默认大家已经安装好jdk与tomcat,所以在这里不做 ...

  4. Mac系统下 PHP7安装Swoole扩展 教程

    转载自 https://www.fujieace.com/php/php-extensions/swoole.html 今天我用的PHP版本是:PHP7.1 环境依赖: php-5.3.10 或更高版 ...

  5. JUnit单元测试代码

    package com.storage.test; import org.junit.Before; import org.junit.Test; import org.springframework ...

  6. 四、UI开发之核心基础——约束(实用)

    概述 本节将会介绍最常用的几种约束,基本可以满足90%以上的UI布局要求. 先附上一份其他优秀博客https://blog.csdn.net/companion_1314/article/detail ...

  7. pycharm中代码整体缩进

    整体缩进 :  1.选中需要缩进的代码 2.Tab键 反向缩进:  shift+Tab

  8. WIN7虚拟桌面创建(多屏幕多桌面)

    Windows7/WIN7虚拟桌面怎么用怎么创建多桌面(摘录) 在使用电脑中经常会遇到桌面软件太多了不够用的感慨,那么要是一台电脑有多个桌面就好了.在windows10中自带已经支持了虚拟桌面,在wi ...

  9. python操作excel的读、计算、写----xlrd、copy

    import xlrd from xlutils.copy import copy class ExcelUtil: def __init__(self,excel_path=None,index=N ...

  10. linux 创建用户和添加到组

    1.添加用户 先用root用户登录 useradd -m testuser #这样的做会在/home下创建目录 2.指定shell #cat /etc/passwd  #查看用户指定shell roo ...