Springboot starts to automatically open the browser

Springboot starts to automatically open the browser

I started the project many times, but I don't know which link to visit to find swagger or the homepage. Configure it here.

Insert picture description here

Configure it in the setting. Google Chrome is opened by default here.

It's very simple, just go to the code, the project used is the one written before: SpringBoot integrates knife4j with this project to demonstrate!

application.properties

isOpen: Whether to open an account and automatically open the browser
cmd: The command executed. Note that there must be a space
openUrl: the automatically opened browser access link

server.port=2080

# 启动项目自动打开浏览器
openProject.isOpen=true
openProject.cmd=cmd   /c   start
openProject.web.openUrl=http://localhost:${
    
    server.port}/doc.html#/

RunConfig

Create a new RunConfig, implement CommandLineRunner, and rewrite the method

package com.zjy.knife4j.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RunConfig implements CommandLineRunner {
    
    

    @Value("${openProject.isOpen}")
    private boolean isOpen;

    @Value("${openProject.web.openUrl}")
    private String openUrl;

    @Value("${openProject.cmd}")
    private String cmd;

    @Override
    public void run(String... args){
    
    
        if(isOpen){
    
    
            String runCmd = cmd + " " + openUrl ;
            System.out.println("运行的命令: " + runCmd);
            Runtime run = Runtime.getRuntime();
            try {
    
    
                run.exec(runCmd);
                System.out.println("启动浏览器打开项目成功");
            } catch (Exception e) {
    
    
                e.printStackTrace();
                System.out.println("启动项目自动打开浏览器失败");
            }
        }
    }
}

The code is finished. You can start!
The console prints the result:
Insert picture description here

Browsers that open automatically:
Insert picture description here

Test OK!
Welcome to the guidance of the great god, you can leave a message to communicate!

======================
My original article, reprinted to indicate the discrepancy!

=================

Guess you like

Origin blog.csdn.net/dayonglove2018/article/details/108452918