首页 > 解决方案 > 如何在 Spring MVC 中记录传入请求

问题描述

我有一个这样的 JSON 请求正文

{  
   "3DSecure":{  
      "paRes":"xxxxxxxxxxxxxxxxxxxxxxx"
   }
}

我开发了端点来记录该请求。

@RequestMapping(value = "/logging/requests", method = RequestMethod.POST)
@ResponseBody
public String loggingIncomingRequest(@RequestBody AxiPayCargilsRequest request,
                                         HttpServletRequest servletRequest,
                                         ServiceContext serviceContext){

    log.info("!--REQUEST START--!"+request);

    log.info("");
    return null;
}

如何记录这个请求任何人都可以从对象创建的层面帮助我谢谢。

标签: javaspringspring-mvc

解决方案


首先,在 pom.xml 中添加以下依赖项

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.2</version>
  </dependency>

然后,在所有需要记录的类中初始化记录器,例如,

private final static Logger logger  = LogManager.getFormatterLogger(YourClassName.class);

现在,您可以像这样使用记录器,

logger.info("Your Logging info");

推荐阅读