有没有一种方法可以在没有“ HttpServletRequest”的情况下使用spring-boot Restful来检索客户端的请求和其他请求? 例如,要拦截对服务器的调用,必须进行扫描,而在没有Servlet API支持的情况下怎么做呢?
有点奇怪,但实际上不知道是否已经实现了此功能的调用转换。是否可以通过遵循注释定义来完成?
@RequestMapping
谢谢:)
答案 0 :(得分:0)
如果您在1.2-2.0之间使用Spring Boot版本 您可以使用Spring Boot Actuator跟踪端点。 您可以看到最近100个带有跟踪端点的传入请求。
将此依赖项添加到pom文件中。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在application.properties中添加此配置。
endpoints.actuator.enabled=true
endpoints.trace.enabled=true
endpoints.trace.sensitive=false
management.trace.include=request-headers,response-headers,cookies,errors,parameters
然后发出请求http://yourhost:yourport/trace(例如http://localhost:8080/trace) 查看日志。
如果要保存日志,
您可以创建自定义跟踪存储库:
@Component
public class customTrace implements TraceRepository {
private final TraceRepository lastOneHundretLogs = new InMemoryTraceRepository();
@Override
public List<Trace> findAll() {
return null;
}
@Override
public void add(Map<String, Object> map) {
}
}
logs位于lastOneHundretLogs对象中。您可以使用它。
或者,您可以使用记录器记录请求
@Component
public class InMemoryTraceRepository implements TraceRepository {
private static final Logger HTTP_LOGGER = LoggerFactory.getLogger("http-logger");
// For security matters it's better to not expose Traces on HTTP
@Override
public List<Trace> findAll() {
return null;
}
@Override
public void add(Map<String, Object> map) {
Trace trace = new Trace(new Date(), map);
String traceInfo = trace.getInfo().toString();
HTTP_LOGGER.info(traceInfo);
}
}