Spring Boot启动时添加访问地址和Swagger地址输出

微笑绽放 2024-02-23 ⋅ 24 阅读

Spring Boot是一个快速开发Java应用程序的框架,它提供了很多便捷的功能和组件。在我们使用Spring Boot开发应用时,经常需要知道应用程序的访问地址和Swagger API文档地址,方便我们进行测试和调试。

本文将介绍如何在Spring Boot启动时添加访问地址和Swagger地址输出,并对标题进行美化。

添加访问地址输出

在Spring Boot应用程序启动时,可以通过自定义一个ApplicationListener来监听应用程序的启动事件,然后在事件的回调方法中输出访问地址。

首先,创建一个ApplicationReadyEventListener类,并实现ApplicationListener<ApplicationReadyEvent>接口。

@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

    @Value("${server.port}")
    private int port;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        String contextPath = event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
        String localhostUrl = "http://localhost:" + port + contextPath;
        String ipAddress = InetAddress.getLocalHost().getHostAddress();
        String ipAddressUrl = "http://" + ipAddress + ":" + port + contextPath;

        System.out.println("访问地址: " + localhostUrl);
        System.out.println("访问地址: " + ipAddressUrl);
    }
}

ApplicationReadyEventListener类中,我们通过@Component注解将其注册为一个Bean,并使用@Value注解注入应用程序的端口号。然后,我们在onApplicationEvent方法中获取应用程序的上下文路径,然后拼接成完整的访问地址,并输出到控制台。

接下来,在application.properties文件中配置应用程序的端口号和上下文路径。

server.port=8080
server.servlet.context-path=/myapp

这样,当我们启动Spring Boot应用程序时,就会在控制台上看到类似以下的输出结果:

访问地址: http://localhost:8080/myapp
访问地址: http://192.168.0.100:8080/myapp

添加Swagger地址输出

在Spring Boot中,我们可以使用Swagger来生成和展示API文档,方便我们进行接口的测试和调试。类似地,我们也可以在应用程序启动时输出Swagger的访问地址。

首先,确保在pom.xml文件中添加了Swagger的依赖。

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

然后,创建一个SwaggerConfig类,并添加@EnableSwagger2注解启用Swagger。

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${server.port}")
    private int port;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.myapp.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @PostConstruct
    public void init() {
        String contextPath = ServletUriComponentsBuilder.fromCurrentContextPath().toUriString();
        String swaggerUrl = "http://localhost:" + port + contextPath + "/swagger-ui.html";

        System.out.println("Swagger地址: " + swaggerUrl);
    }
}

SwaggerConfig类中,我们使用@Value注解注入应用程序的端口号。然后,在init方法中,通过ServletUriComponentsBuilder获取应用程序的上下文路径,并拼接成Swagger的访问地址,并输出到控制台。

最后,我们需要将SwaggerConfig类添加到扫描的包路径中。

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.myapp", "com.example.myapp.config"})
public class MyAppApplication {
    // ...
}

启动Spring Boot应用程序后,在控制台上会看到类似以下的输出结果:

Swagger地址: http://localhost:8080/myapp/swagger-ui.html

结论

通过在Spring Boot启动时添加访问地址和Swagger地址输出,我们可以方便地获取应用程序的访问地址和Swagger API文档地址,提高开发和调试的效率。

希望本文对你有所帮助,祝你在Spring Boot的开发中取得更好的成果!


全部评论: 0

    我有话说: