MENU

springboot配置swagger2笔记

March 15, 2021 • 开发

①pom.xml

<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>

②创建一个SwaggerConfig.java

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * @author swagger
 */
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.demonation.demo"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Xupt API文档")
                .description("power by swagger")
                .termsOfServiceUrl("https://www.zhai78.com")
                .version("1.0")
                .build();
    }
}

③在项目的启动程序上加上注释@EnableSwagger2

@SpringBootApplication
@MapperScan("com.demonation.demo.dao")
@EnableSwagger2
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

④启动程序,访问swagger-ui

http://localhost:8080/swagger-ui.html

转载请注明出处,并添加原文地址

Archives QR Code Tip
QR Code for this page
Tipping QR Code