Launch browser automatically after spring-boot webapp is ready

后端 未结 6 890
失恋的感觉
失恋的感觉 2020-12-15 09:43

How do I launch a browser automatically after starting the spring boot application.Is there any listener method callback to check if the webapp has been deployed and is read

6条回答
  •  天命终不由人
    2020-12-15 10:14

    I've recently been attempting to get this working myself, I know it's been a while since this question was asked but my working (and very basic/simple) solution is shown below. This is a starting point for anyone wanting to get this working, refactor as required in your app!

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.awt.*;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
            openHomePage();
        }
    
        private static void openHomePage() {
            try {
                URI homepage = new URI("http://localhost:8080/");
                Desktop.getDesktop().browse(homepage);
            } catch (URISyntaxException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题