您可以使用泽西过滤器。
public class HTTPFilter implements ContainerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(HTTPFilter.class);
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
logger.info(containerRequestContext.getUriInfo().getPath() + " endpoint called...");
//logger.info(containerRequestContext.getUriInfo().getAbsolutePath() + " endpoint called...");
}
}
之后你必须在 http 配置文件中注册它或者只是扩展 ResourceConfig 类。这就是你如何在 http config 类中注册它
public class HTTPServer {
public static final Logger logger = LoggerFactory.getLogger(HTTPServer.class);
public static void init() {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9191).build();
ResourceConfig config = new ResourceConfig(Endpoints.class, HTTPFilter.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
logger.info("HTTP Server started");
}
}