Springboot项目修改请求参数

需求:前端传过来两个参数searchStartDate和searchEndDate,但是传的是带日期格式的,不带时分秒的,这就导致传到后台的时间都是00:00:0,searchStartDate倒无所谓,searchEndDate需加上23:59:59。

思路:1.用切面的方式,在切面对controller进行拦截

 

@Component
@Aspect
@Log4j2
public class SearchFormateAspect {

    @Pointcut("execution(public * cn.com.test.controller.*.*(..))")
    public void getMethods() {
    }

    @Before("getMethods()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {

        //获取目标方法的参数信息
        Object[] obj = joinPoint.getArgs();

        for (Object argItem : obj) {
            if (argItem instanceof BaseEntity) {
                BaseEntity baseEntity = (BaseEntity) argItem;
                // 前端传入年月日,searchEndDate拼接23:59:59
                Date searchEndDate = baseEntity.getSearchEndDate();
                if (searchEndDate != null) {
                    searchEndDate = DateUtils.setHours(searchEndDate,23);
                    searchEndDate = DateUtils.setMinutes(searchEndDate,59);
                    searchEndDate = DateUtils.setSeconds(searchEndDate,59);
                    baseEntity.setSearchEndDate(searchEndDate);
                }
            }
        }
    }
}

2.过滤器,自定义一个过滤器,自定义wrapper实现HttpServletRequestWrapper

@Component
@Slf4j
public class SearchDateFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain chain) throws IOException, ServletException {

        // 获取你需要的参数
        String searchStartDate = request.getParameter("searchStartDate");
        String searchEndDate = request.getParameter("searchEndDate");
        if (StringUtils.isBlank(searchStartDate) && StringUtils.isBlank(searchEndDate)) {
            chain.doFilter(request, response);
        } else {
            RequestParameterWrapper wrapper = new RequestParameterWrapper(request);
            chain.doFilter(wrapper, response);
        }
    }

}
public class RequestParameterWrapper extends HttpServletRequestWrapper {
    private Map params = new HashMap<>();

    public RequestParameterWrapper(HttpServletRequest request) {
        super(request);
        this.params.putAll(this.getParameterMap());
    }

    /**
     * 重写getParameter,代表参数从当前类中的map获取
     *
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        String[] values = params.get(name);
        if (values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }

    @Override
    public Map getParameterMap() {
        HashMap newMap = new HashMap<>();
        newMap.putAll(super.getParameterMap());
        String[] searchEndDate = newMap.get("searchEndDate");
        if(searchEndDate != null && !StringUtils.isBlank(searchEndDate[0])){
            newMap.put("searchEndDate", new String[]{searchEndDate[0] + " 23:59:59"});
        }
        return Collections.unmodifiableMap(newMap);
    }

    /**
     * 同上
     *
     * @param name
     * @return
     */
    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }
}

你可能感兴趣的:(解决方案,springboot,过滤器,切面)