AOP中拦截Controller中的参数值

1.获取普通的传参

eg: http://…url…?param=value
此类形式的AOP参数获取可以用以下方式

@Before("StuInfoAsp()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes sc = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sc.getRequest();
         logger.info("uri--"+request.getRequestURI());
        logger.info("port--"+request.getServerPort());
        logger.info("ip--"+request.getRemoteHost());
        logger.info("method"+request.getMethod());
		logger.info("value"+request.getQueryString());
    }

2.获取@PathVariable中的传参

eg:http://…url/value
发现上面的方式获取不到,于是采用以下方法

Controller

@GetMapping("/{word}")
    @ApiOperation(value = "通过学号或者姓名找到学生信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "word",value = "姓名或学号",required = true,dataType = "String")})
    public List<StuInfo> getOneStu(@PathVariable("word") String word){
        return stuInfoDao.findStuByNameOrNo(word);
    }

AOP

@Before("StuInfoAsp()")
    public void doBefore(JoinPoint joinPoint){
    	ServletRequestAttributes sc = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sc.getRequest();
        Map map = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        String word = map.get("word").toString();
        logger.info(word);
    }

记得点赞加关注o!!!

你可能感兴趣的:(荆棘之路,aop,spring,boot)