如何查看当前SpringBoot使用的何种web服务器呢?
下文笔者讲述查看SpringBoot中web日志服务器查看方法,如下所示
Spring Boot 使用web日志服务器查看方法
方式1: 查看SpringBoot启动日志 方式2: mvn dependency:tree 查看引用的jar类型 方式3: 检测pom.xml文件 方式4: 使用Spring Boot Actuator 方式5: 使用编程的方式,查看当前服务器类型例
1.查看启动日志
当Spring Boot应用程序启动时 它会在控制台输出启动日志 其中包括使用的Web服务器信息 例 - Tomcat: Tomcat started on port(s): 8080 (http) with context path '' -Jetty: Jetty started on port(s) 8080 (http/1.1) with context path '' - Undertow Undertow started on port(s) 8080 (http) with context path ''
2.使用`mvn dependency:tree`命令
mvn dependency:tree - Tomcat: [INFO] org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.4:compile - Jetty: [INFO] org.springframework.boot:spring-boot-starter-jetty:jar:2.7.4:compile - Undertow: [INFO] org.springframework.boot:spring-boot-starter-undertow:jar:2.7.4:compile
3.检查`pom.xml`文件
例:- Tomcat: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> - Jetty: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> - Undertow: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
4.使用Spring Boot Actuator
项目中集成Spring Boot Actuator 可通过访问`/actuator/info`或`/actuator/env`端点来获取更多信息例:
访问`/actuator/env`: GET http://localhost:8080/actuator/env
5.编程方式检查服务器
例import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import javax.servlet.ServletContext; public class WebServerChecker { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(MyApplication.class, args); if (context instanceof ServletWebServerApplicationContext) { ServletContext servletContext = ((ServletWebServerApplicationContext) context).getServletContext(); if (servletContext != null) { System.out.println("Web Server: " + servletContext.getServerInfo()); } } else { WebHttpHandlerBuilder builder = context.getBean(WebHttpHandlerBuilder.class); System.out.println("Web Server: " + builder.getWebServerFactory().getClass().getSimpleName()); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。