Java Swagger Java Swagger技术使用指南

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java Swagger Java Swagger技术使用指南

四季人06   2021-09-30 我要评论
想了解Java Swagger技术使用指南的相关内容吗,四季人06在本文为您仔细讲解Java Swagger的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,Swagger,下面大家一起来学习吧。

Swagger的作用与概念

Swagger官网,点此进入
在前后端分离时代,我们需要实时自动更新接口信息,和测试接口,实现前后端分离式开发,swagger因此产生

在项目中使用swagger

以下以3.0.0依赖为例

 <!--swagger 相关组件-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
          <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-boot-starter</artifactId>
           <version>3.0.0</version>
        </dependency>
@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
         return "hello";
    }
}
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {

}

然后访问http://localhost:8080/swagger-ui/index.html
你就能看到如下界面,为swagger文档

在这里插入图片描述

配置swagger

先来看看底层的代码,了解一下

在这里插入图片描述

在这里插入图片描述

ApiInfo 配置

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

    @Bean
    public Docket docket(){

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

    //配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("宋先慧", "https://blog.csdn.net/sxh06", "xianhuisong@yeah.net");
        return new ApiInfo(
                "宋先慧的Api Documentation",
                "学习swagger没有尽头",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

swagger配置扫描接口

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

//    @Bean
//    public Docket docket1(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("分组二");
//    }
    @Bean
    public Docket docket(Environment environment){


        Profiles profiles=Profiles.of("dev");
        //获取项目的环境
         boolean flag=environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("宋先慧")  //分组
                .enable(flag)  //enable 配置是否启动swagger  flase则不能在浏览器访问
                .select()
                //RequestHandlerSelectors实现类  配置扫描方式
                // basePackage指定要扫描的包
                // any()全部
                // none()都不扫描
                //withClassAnnotation()  扫描类上的注解  参数是一个注解的反射对象
                //withMethodAnnotation   扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.sxh.swagger.controller"))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                //过滤什么路劲  过滤请求
                //.paths(PathSelectors.ant("/sxh/**"))
                .build();
    }

    //配置swagger信息apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("宋先慧", "https://blog.csdn.net/sxh06", "xianhuisong@yeah.net");
        return new ApiInfo(
                "宋先慧的Api Documentation",
                "学习swagger没有尽头",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}

如果我只希望在生成环境使用swagger,在正式环境不使用swagger怎么解决?(enable=false|true)

配置api文档分组

在这里插入图片描述

多个分组

配置多个Docket 实例即可

 @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("分组一");
    }

 @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("分组二");
    }`

实体类配置

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们