Java教程

Spring Swagger

本文主要是介绍Spring Swagger,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    //配置了Swagger的docket实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage 指定要扫描的包
                //.any() 扫描全部
                //.none() 都不扫描
                //.withClassAnnotation() 扫描类上注解      参数是一个注解的反射对象
                //.withMethodAnnotation() 扫描方法上的注解  比如RestController
                .apis(RequestHandlerSelectors.basePackage("com.lin.springswagger.controller"))
                //.path() 过滤什么路径  只扫描
                //.paths(PathSelectors.ant("/ccgg/**"))
                .build();
    }

    //配置自己的Swagger信息
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("ccgg", "www.ccgg.com", "ccgg@qq.ocm");
        return new ApiInfo(
                "ccgg!",
                "再小的帆也能远航",
                "v1.0",
                "https://www.ccgg.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}

配置是否启动Swagger

代码

/**
 * @Author lin
 * @Date 2020/10/20 22:48
 */
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    //配置了Swagger的docket实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        //通过environment.acceptsProfiles 判断是否自己处在设定的环境
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启swagger默认是开启的
                .enable(flag)
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage 指定要扫描的包
                //.any() 扫描全部
                //.none() 都不扫描
                //.withClassAnnotation() 扫描类上注解      参数是一个注解的反射对象
                //.withMethodAnnotation() 扫描方法上的注解  比如RestController
                .apis(RequestHandlerSelectors.basePackage("com.lin.springswagger.controller"))
                //.path() 过滤什么路径  只扫描
                //.paths(PathSelectors.ant("/ccgg/**"))
                .build();
    }

    //配置自己的Swagger信息
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("ccgg", "www.ccgg.com", "ccgg@qq.ocm");
        return new ApiInfo(
                "ccgg!",
                "再小的帆也能远航",
                "v1.0",
                "https://www.ccgg.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}

实体类配置

1.无任何配置

2.实体类添加文档注释

3.方法添加注释

4.入参注释


测试功能

这篇关于Spring Swagger的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!