如何在`@ Aspect`中获取请求参数?

时间:2016-06-16 08:34:02

标签: java spring spring-mvc aop

我试图在每个请求上添加日志来打印IP,时间,请求参数,响应正文等。这是我的代码:

@Around("execution(* cn.dogchao.carcare.web.controller..*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes sra = (ServletRequestAttributes)ra;
    HttpServletRequest request = sra.getRequest();

    // always empty!
    inputParamMap = request.getParameterMap();
    // uri
    requestPath = request.getRequestURI();

    fromIP = getIpAddr(request);

    // 
    outputParamMap = new HashMap<>();
    Object result = pjp.proceed();// 
    outputParamMap.put("result", result);

    return result;
}

我有Controller这样:

@Controller("channelController")
@RequestMapping(value = "/channel")
public class ChannelController {
    @Autowired
    private ChannelService channelService;
    /**
     * 
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/salesChannelAdd")
    public String salesChannelAdd(@RequestParam Map<String, String> params) {
        ChannelVO vo;
        try {
            vo = JSON.parseObject(JSON.toJSON(params).toString(), ChannelVO.class);
        }catch (Exception e){
            return JSON.toJSONString(new ResultJson(ErrorCode.INVALID_PARAM, "参数异常:"+e.getCause()).getResultMap());
        }

        if(!vo.validate()){
            return JSON.toJSONString(new ResultJson(ErrorCode.INVALID_PARAM, vo.getErrorMsg()).getResultMap());
        }
        return JSON.toJSONString(channelService.createChannel(vo));
    }
}

现在我打印除了参数之外的所有内容。我有一个空的参数图。我不知道为什么它是空的,以及如何获得所有参数。

0 个答案:

没有答案