Springboot項目啓動完成後,自動打開瀏覽器的配置

1. 增加命令行執行類

[@Component](https://my.oschina.net/u/3907912)
public class StartSuccessful implements CommandLineRunner {
    @Value("${open.browser.url}")
    private String url = "http://localhost:8001/swagger-ui.html";

    [@Override](https://my.oschina.net/u/1162528)
    public void run(String... args) throws Exception {
        System.out.println("開始自動加載指定的頁面");
        try {
            //可以指定自己的路徑
            Runtime.getRuntime().exec("cmd   /c   start   " + url);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

增加在yml中添加配置

# 啓動之後要打開的地址
open:
  browser:
    url: http://localhost:8001/swagger-ui.html

2. 直接啓動了添加

try {
	InetAddress addr = InetAddress.getLocalHost();
	System.out.println("Local HostAddress: "+addr.getHostAddress());
	Runtime.getRuntime().exec("cmd   /c   start   http://"+addr.getHostAddress()+":8080/files");//可以指定自己的路徑
} catch (Exception ex) {
	ex.printStackTrace();
}

來源: https://blog.csdn.net/weixin_43852058/article/details/111058061 https://blog.csdn.net/xiaoerbuyu1233/article/details/122962375

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章