1. 程式人生 > >spring-boot整合3:整合swagger

spring-boot整合3:整合swagger

Why swagger?

使用swagger讓你從編寫介面文件的無聊工作中解脫出來

 

1.Maven依賴

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

  

2.Swagger配置類

使用自定義Swagger配置類可以自定義一些swagger配置,比如設定要掃描的base package,也可以不用Swagger配置類,使用預設配置。

package com.zhya.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger 配置類
 *
 * @Author zhangyang
 * @Date 下午 8:57 2018/11/20 0020
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket getDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhya.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

 

3.例項