栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swagger

背景

前后端分离
前端 → 前端控制层、视图层
后端 → 后端控制层、服务层、数据访问层
前后端通过API进行交互
前后端相对独立且松耦合

产生的问题
前后端集成,前端或者后端无法做到 “及时协商,尽早解决”,最终导致问题集中爆发

解决方案
首先定义schema [ 计划的提纲 ],并实时跟踪最新的API,降低集成风险

Swagger

号称世界上最流行的API框架
Restful Api 文档在线自动生成器 → API 文档 与API 定义同步更新
直接运行,在线测试API
支持多种语言 (如:Java,PHP等)
官网:https://swagger.io/

SpringBoot集成Swagger

在项目中使用Swagger需要springbox

  • Swagger 2
  • UI
使用Swagger

1、新建一个SpringBoot-web项目

2、添加Maven依赖


   io.springfox
   springfox-swagger2
   2.9.2



   io.springfox
   springfox-swagger-ui
   2.9.2

3、编写HelloController,测试确保运行成功!

4、要使用Swagger,我们需要编写一个配置类-SwaggerConfig来配置 Swagger

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

5、访问测试 :
输入 http://localhost:8080/swagger-ui.html ,可以看到swagger的界面;

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

    //配置了Swagger的Docket的Bean实例
    @Bean
    public Docket docket(){
        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger信息 = apiInfo
    private ApiInfo apiInfo(){

        return new ApiInfo(
                "Acoffee的文档",
                "遍历山河,觉得人间值得",
                "1.0",
                "https://blog.csdn.net/weixin_44742328",
                 new Contact("Acoffee", "https://blog.csdn.net/weixin_44742328", "763857320@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}


其实这里面需要我们去配置的东西就这么一点点,就算你不配置你也要占位因为ApiInfo这个方法里面是没有get方法的。

Swagger配置扫描接口
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

    //配置了Swagger的Docket的Bean实例
    @Bean
    public Docket docket(){

        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(false)//enable是否启动Swagger,如果为false,则Swagger不能再浏览器中访问
                //.select().apis().build()是一体的
                .select()
                //RequestHandlerselectors,配置要扫描接口的方式
                // basePackage:指定要扫描的包
                // any ():扫描全部
                //none():不扫描
                //withclassAnnotation:扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation:扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.acoffee.swaggerdemo"))
                //过滤什么路径
                //.paths(PathSelectors.ant("/com
@Configuration
@EnableSwagger2 
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){

        //设置需要显示Swagger的开发环境:这里就是如果是生产环境就显示出来,如果是其他环境就不显示
        Profiles value = Profiles.of("pro");
        //通过environment.acceptsProfiles判断是否处于需要显示Swagger的环境中
        boolean flag = environment.acceptsProfiles(value);//如果是test或者dev环境就为true


        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //我们将上面从Profiles拿到的flag传入,从而来控制Swagger是否显示出来
                .enable(flag)//enable是否启动Swagger,如果为false,则Swagger不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.acoffee.swaggerdemo"))
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfo(
                "Acoffee的文档",
                "遍历山河,觉得人间值得",
                "1.0",
                "https://blog.csdn.net/weixin_44742328",
                 new Contact("Acoffee", "https://blog.csdn.net/weixin_44742328", "763857320@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
配置API文档的分组

使用groupName("A")进行分组

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

    //每一个人都有有自己对应的一个Swagger页面
    @Bean
    public Docket docket1(){
        return new Docket(documentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(documentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(documentationType.SWAGGER_2).groupName("C");
    }

    @Bean
    public Docket docket(Environment environment){

        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .groupName("aocffee")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.acoffee.swaggerdemo"))
                .build();
    }

    //配置Swagger信息 = apiInfo
    private ApiInfo apiInfo(){
    
        return new ApiInfo(
                "Acoffee的文档",
                "遍历山河,觉得人间值得",
                "1.0",
                "https://blog.csdn.net/weixin_44742328",
                new Contact("Acoffee", "https://blog.csdn.net/weixin_44742328", "763857320@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

Acoffee对应的Swagger页面

A对应的Swagger页面

标注字段别名
@Data
@ApiModel("用户实体类")
public class User {

    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}
@RestController
@Api(description = "控制类")
public class helloController {

    @GetMapping(value = "/hello")
    public String hello() {
        return "hello";
    }

    //只要在我们的接口中,返回值中存在实体类,它就会扫描到Swagger中
    @GetMapping(value = "/user")
    public User user() {
        return new User();
    }
    
    @ApiOperation("hello控制类")
    @PostMapping(value = "/hello2")
    public String hello2(@ApiParam("用户名") String username) {
        return "hello" + username;
    }

    @ApiOperation("post测试类")
    @PostMapping(value = "/post1")
    public User post1(@ApiParam("用户名") User user){
        return user;
    }
}



这只是swagger一个简单的功能,并不是的它的最厉害的地方,它的强大的地方是它还可以做测试,就像postman那样。

测试功能


输入username和password过后点击execute,下面就会显示执行信息,和对应的响应码


总结:
1.我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
2.接口文档实时更新
3.可以在线测试

在正式发布的时候我们一般都关闭Swagger,出于安全问题,最后一句Swagger就是因为现在前后端分离所出现的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号