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

         在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就比较方便。

你可能感兴趣的:(spring,boot,java,后端)