spring boot 启动类 快捷入口

该博客介绍了如何在项目启动时通过配置参数自动获取并显示本地和外部访问URL,避免频繁手动输入端口号。通过SpringApplication运行时获取`server.port`配置,结合主机IP地址,构建完整的访问路径,包括Swagger的API文档URL。此解决方案简化了项目的启动和调试过程。

项目场景:

为了减少项目启动后经常输入端口号的操作,结合配置参数在启动时新增链接地址

解决方案:

参数分析:application.getEnvironment().getProperty(“server.port”);
里的server.port就是对应配置类里的对应配置
在这里插入图片描述

	public static void main(String[] args) throws UnknownHostException {
		//SpringApplication.run(CarHostApplication.class, args);

        ConfigurableApplicationContext application = SpringApplication.run(CarHostApplication.class, args);
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port =application.getEnvironment().getProperty("server.port");
        String property =application.getEnvironment().getProperty("server.servlet.context-path");
        String path = property == null ? "" :  property;
        System.out.println(
                "\n\t" +
                        "----------------------------------------------------------\n\t" +
                        "Application Sailrui-Boot is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                        "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
						"Swagger: \thttp://" + ip + ":" + port  + "/swagger-ui.html\n\t" +
                        "------------------------------------------------------------");

	}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值