SPRINGBOOT HTTP REQUEST 打印
- 参考方案
- 简单办法
- 环境允许tcpdump更简单
参考方案
https://www.baeldung.com/spring-http-logging
https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl
简单办法
@Controller
@RequestMapping("/**")
@Slf4j
public class CaptureNetController {@RequestMapping(value = "")@SneakyThrowspublic ResponseEntity<Void> capture(HttpServletRequest request) {var mapper = new ObjectMapper();mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);var lines = IOUtils.readLines(request.getInputStream());log.info("url:{}", request.getRequestURI());log.info("method:{}", request.getMethod());log.info("body:{}", lines);log.info("parameters:{}", mapper.writeValueAsString(request.getParameterMap()));if (request instanceof MultipartRequest mpRequest) {log.info("files:{}", mapper.writeValueAsString(mpRequest.getFileNames()));}log.info("=======================================================================");return ResponseEntity.ok(null);}
}