springboot启动项目时自动启动swaggerUI.html页面

1.首先我们得在maven添加swaggerUI依赖(在pom.xml文件中)

 
            io.springfox
            springfox-swagger2
            2.2.2
        

2.在controller层添加swaggerui接口注解


@RestController
@RequestMapping(value = "/api")
@Api(value = "文章表接口", tags = "文章表接口", description = "")
public class ArticleController {



    @ApiOperation(value = "通过ID获取单个文章表", notes = "通过id获取文章信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error")})
    @RequestMapping(value = "/v1/article/select/{id}", method = RequestMethod.GET)
    public Result getEntity(@ApiParam(value = "用户ID", required = true) @PathVariable(value = "id") Integer id) {
        try {
            ArticleEntityView articleEntityView = articleService.getEntity(id);
            return ResultUtil.success(ResultUtil.SUCCESS_CODE_200, articleEntityView);
        } catch (Exception e) {
            return ResultUtil.error(ResultUtil.SUCCESS_CODE_200, e.getMessage());
        }
    }

3.在启动类添加注解

@SpringBootApplication
@EnableScheduling
@EnableSwagger2
public class NbtApplication {

    public static void main(String[] args) {
        SpringApplication.run(NbtApplication.class, args);
    }

}

现在我们启动项目然后输入localhost:端口/swagger-ui.html便可以访问我们要生成的接口了,但是,我要的是自动打开,自动打开,自动打开,所以我们还得在配置一个东西

4.新建一个类,用于启动项目时候自动生成时候自动运行一次(我喜欢将这些类放到config包下),一些配置如果常改的,可以放到配置项中,然后依赖注入进来


#本地浏览器地址
spring.web.googleexcute=explorer
#要打开的网址
spring.web.loginurl=http://localhost:${server.port}/swagger-ui.html
#是否要启动时打开浏览器
spring.web.isopenurl=true

/**
 * 项目启动时自动打开http://localhost:9036/swagger-ui.html
 */
@Component
public class StepExecutorConfig  implements ApplicationRunner {
    private static final Logger LOG = LoggerFactory.getLogger(StepExecutorConfig.class);

    @Value("${spring.web.loginurl}")
    private String loginUrl;

    @Value("${spring.web.googleexcute}")
    private String googleExcutePath;

    @Value("${spring.web.isopenurl}")
    private boolean isOpen;
    @Override
    public void run(ApplicationArguments args){
        //先判断当前环境
        if (!exitisVersion()){
            //启动swagger-ui
            application();
        }
    }

    private Boolean exitisVersion() {
        String osName = System.getProperties().getProperty("os.name");
        if(osName.equals("Linux")) {
            LOG.info("running in Linux");
            return true;
        }
        else{
            LOG.info("don't running in Linux");
            return false;
        }
    }

    private void application() {

        if (isOpen){
            String cmd = googleExcutePath +" "+ loginUrl;
            LOG.info("浏览地址:" + cmd);
            Runtime run = Runtime.getRuntime();
            try{
                run.exec(cmd);
                LOG.info("启动浏览器打开项目成功");
            }catch (Exception e){
                e.printStackTrace();
                LOG.error(e.getMessage());
            }
        }
    }
}

接下来就启动项目时在Windows环境下网页就自动打开了

你可能感兴趣的:(Springboot)