1. 导入依赖

<!-- swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2. 编写配置类 SwaggerConfig.java 自己看着改改就行(可选)

package com.sage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.sage.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SwaggerApi - Sage")
                .description("SwaggerApi")
                .contact(new Contact("Sage", "https://www.sage.run/", "xxx"))
                .version("1.0.0")
                .build();
    }
}

3. 主入口加上注解 @EnableOpenApi

@EnableOpenApi
@SpringBootApplication
public class SwaggerStudyApplication {

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

}

4. 访问 http://localhost:8080/swagger-ui/index.html

1.png

最后编辑:2020年11月12日 ©著作权归作者所有

发表评论