Spring boot 启动添加访问地址和swagger地址输出

2024-01-09 16:47:40

? ? ? ? ?在Spring boot 项目启动后,输出访问地址和swagger地址,便于查看和对接。

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@MapperScan(basePackages = {"com.study.**.repository", "com.study.**.dao", "com.study.**.mapper"})
@Slf4j
public class StaffMailboxApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(StaffMailboxApplication.class, args);
        Environment env = application.getEnvironment();
        log.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n\t" +
                        "Doc: \thttp://{}:{}{}/doc.html\n" +
                        "\t---Staff Mailbox started successfully\n" +
                        "\t---启动成功---\n" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path"));
    }

}

通过Environment去读取配置的名称,端口和路径。 启动后,就可以看到输出的内容,可以直接访问swagger就比较方便。

文章来源:https://blog.csdn.net/qq_35461948/article/details/135482906
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。