项目场景:
为了减少项目启动后经常输入端口号的操作,结合配置参数在启动时新增链接地址
解决方案:
参数分析: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" +
"------------------------------------------------------------");
}
}

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

992

被折叠的 条评论
为什么被折叠?



