spring boot swagger配置

1.首先引入包

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2.增加配置文件 /config/SwaggerConfig

package com.example.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author 兵兵有你
 * @date 2021/11/2 0002 9:57
 * @Email 1360968945@qq.com
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment){
        //检查有没有application_dev.properties或application_test.properties文件,有则为true,可以只写一个文件
        Profiles profiles = Profiles.of("dev","test");
        Boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .enable(flag) //true则显示文档
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))//要扫描接口的目录
                .build();
    }

    private ApiInfo apiInfo(){

        return new ApiInfo(
                "后台文档",
                "这是文档描述",
                "1.2",
                "http://localhost:8080",
                new Contact("bb","aa","cc@qq.com"),
                "22",
                "",new ArrayList<>());
    }
}

3.然后访问localhost:8080/swagger-ui.html即可访问

原文地址:https://www.cnblogs.com/bing2017/p/15502129.html