springBoot項目啟動時,在控制台直接打印出swagger文檔鏈接


1、添加swagger配置

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * 控制台輸出 Swagger 接口文檔地址
 **/
@Component
@Slf4j
public class SwaggerPrintConfig implements ApplicationListener<WebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            // 獲取IP
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            // 獲取端口號
            int port = event.getWebServer().getPort();
            // 獲取應用名
            String applicationName = event.getApplicationContext().getApplicationName();
            // 打印 swagger 文檔地址
            log.info("項目啟動啟動成功!swagger 接口文檔地址: http://" + hostAddress + ":" + port + applicationName + "/swagger-ui.html");
            // 打印 swagger2 文檔地址
            log.info("項目啟動啟動成功!swagger2 接口文檔地址: http://" + hostAddress + ":" + port + applicationName + "/doc.html");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

2、查看配置后的效果

 

 3、點擊鏈接即可進入對應的文檔中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM