Swagger2是什么?@EnableSwagger2注解?

转自:http://ju.outofmemory.cn/entry/355158

首先,引入maven依赖


  io.springfox
  springfox-swagger2
  2.7.0



  io.springfox
  springfox-swagger-ui
  2.7.0

Swagger2是一个效率工具

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* Created by Trace on 2018-05-16.
* Desc: swagger2配置类 */ @SuppressWarnings({"unused"}) @Configuration @EnableSwagger2 public class Swagger2Config { @Value("${swagger2.enable}") private boolean enable; @Bean("用户模块") public Docket userApis() { return new Docket(DocumentationType.SWAGGER_2) .groupName("用户模块") .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.regex("/user.*")) .build() .apiInfo(apiInfo()) .enable(enable); } @Bean("客户模块") public Docket customApis() { return new Docket(DocumentationType.SWAGGER_2) .groupName("客户模块") .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.regex("/custom.*")) .build() .apiInfo(apiInfo()) .enable(enable); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXXXX系统平台接口文档") .description("提供子模块1/子模块2/子模块3的文档, 更多请关注公众号: 随行享阅") .termsOfServiceUrl("https://xingtian.github.io/trace.github.io/") .version("1.0") .build(); } }
import org.springframework.beans.factory.annotation.Value;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2 配置
 * Created by pmdream on 2018/4/20.
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Value("${swagger.show}")
    private boolean swaggerShow;


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.pmdream.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("测试Swagger")
                .termsOfServiceUrl("http://dkreal.cn/")
                .contact("pmdream")
                .version("1.0")
                .build();
    }
}

= =(留着之后填坑)

你可能感兴趣的:(SpringBoot学习)