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即可访问

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
spring boot swagger配置 spring boot swagger配置 spring boot swagger配置 spring boot swagger配置   TRANSLATE with spring boot swagger配置 COPY THE URL BELOW spring boot swagger配置 spring boot swagger配置 Back EMBED THE SNIPPET BELOW IN YOUR SITE spring boot swagger配置 Enable collaborative features and customize widget: Bing Webmaster Portal Back
上一篇:Swagger2和Swagger3用法简介


下一篇:swagger简单使用