我是程序员技术干货

springfox-bridge:随心所欲地为非restful接

2019-03-03  本文已影响133人  DoubleBin

项目github地址: https://github.com/double-bin/springfox-bridge

一、引言

    目前,利用swagger框架为restful接口编写API文档非常流行,在spring web项目中,利用springfox+swagger更是可以通过注解的方式直接进行API文档的生成,这样开发者在项目开发的同时就直接把文档准备好了,利用springfox的配置,可以在项目启动后直接浏览器访问查看API文档,同时还能在界面直接进行API的测试。springfox的使用本文不在此赘述了,现在引出一个问题: 非restful接口能否采用swagger生成接口文档

    在项目中集成springfox-bridge可以快速的为非restful接口生成API文档,编写文档的方式跟springfox一样简单,在相关类或者接口上采用注解的方式定义文档信息即可。

    springfox-bridge相当于架设了一座与springfox之间的桥梁,通过动态生成配置了springfox注解的mvc接口并进行注册,形成对非restful接口生成swagger文档的能力。

二、springfox-bridge特性说明

  1. 启动简单
  1. 兼容性强
  1. 简单的注解
  1. 方便的分组
  1. 不影响原有文档
  1. 方法入参不限定请求体的数量
  1. 支持界面测试

三、使用说明

使用springfox-bridge需要项目本身启用了springmvc框架, spring相关依赖版本在spring3.1以上

3.1 配置maven依赖

1)使用了springboot的项目:

        <dependency>
            <groupId>com.github.double-bin</groupId>
            <artifactId>springfox-bridge-spring-boot-starter</artifactId>
            <version>1.0.1</version>
        </dependency>
  1. 非springboot项目:
        <dependency>
            <groupId>com.github.double-bin</groupId>
            <artifactId>springfox-bridge-core</artifactId>
            <version>1.0.1</version>
        </dependency>

3.2 启动配置

1)使用了springboot的项目

  1. 非springboot项目:
    可通过配置类实现ApplicationContextAware接口的setApplicationContext方法,方法实现中通过SpringfoxBridge.start()方法启动springfox-bridge, 配置类上通过@EnableSwagger2启动springfox基本功能, 可参考:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import com.github.doublebin.springfox.bridge.core.SpringfoxBridge;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@EnableSwagger2
@Configuration
public class MyXXXConfiguration implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringfoxBridge.start(applicationContext);

    }
}

3.3 使用示例

下面示例代码演示如何使用springfox-bridge的注解,如何定义的文档,如果设置分组等,展示结果请看3.4

1, 定义两个请求的model类:TestRquest1和TestRequest2

model类中可以使用springfox的原生注解:io.swagger.annotations.ApiModel和io.swagger.annotations.ApiModelProperty

package com.github.doublebin.springfox.bridge.demo.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel(value="TestRequest1", description = "测试请求体1")
public class TestRequest1 {

    @ApiModelProperty(value = "唯一id", required = true)
    private long uuid;

    @ApiModelProperty(value = "名字", required = true)
    private String name;
}

package com.github.doublebin.springfox.bridge.demo.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel(value="TestRequest2", description = "测试请求体2")
public class TestRequest2 {

    @ApiModelProperty(value = "名字", required = true)
    private String name;

    @ApiModelProperty(value = "描述", required = true)
    private String desc;
}

2, 定义三个service类,并标注@Service供spring扫描并注册bean。
package com.github.doublebin.springfox.bridge.demo.service;

import com.github.doublebin.springfox.bridge.demo.model.TestRequest1;
import com.github.doublebin.springfox.bridge.demo.model.TestRequest2;
import org.springframework.stereotype.Service;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeApi;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeGroup;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeModelProperty;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeOperation;

@Service
@BridgeApi(value = "TestService1 Apis", description = "测试服务1")
@BridgeGroup("test-group1")
public class TestService1 {

    @BridgeOperation(value = "测试查询1", notes = "测试查询方法1说明")
    public String testQuery(@BridgeModelProperty(value = "用户id", required = true) long id, @BridgeModelProperty(value = "请求2", required = false) TestRequest1 request){
        return "Test query success, id is " + id;
    }

    @BridgeOperation(value = "测试查询2", notes = "测试查询方法2说明")
    public String testQuery(@BridgeModelProperty(value = "用户id", required = true) long id){
        return "Test query success, id is " + id;
    }

    @BridgeOperation(value = "测试查询3", notes = "测试查询方法3说明")
    public String testQuery(){
        return "Test query success.";
    }

    @BridgeOperation(value = "测试查询4", notes = "测试查询方法4说明")
    public String testQuery(@BridgeModelProperty(value = "用户id", required = true) long id, @BridgeModelProperty(value = "请求2", required = false) TestRequest2 request){
        return "Test query success, id is " + id;
    }

}
package com.github.doublebin.springfox.bridge.demo.service;

import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeApi;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeGroup;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeModelProperty;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeOperation;
import com.github.doublebin.springfox.bridge.demo.model.TestRequest1;
import org.springframework.stereotype.Service;

@Service
@BridgeApi(value = "TestService2 Apis", description = "测试服务2")
@BridgeGroup("test-group2")
public class TestService2 {
    @BridgeOperation(value = "测试查询", notes = "测试查询方法说明")
    public String testQuery(@BridgeModelProperty(value = "用户id", required = true) long id, @BridgeModelProperty(value = "请求2", required = false) TestRequest1 request){
        return "Test query success, id is " + id;
    }
}

package com.github.doublebin.springfox.bridge.demo.service;

import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeApi;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeGroup;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeModelProperty;
import com.github.doublebin.springfox.bridge.core.builder.annotations.BridgeOperation;
import com.github.doublebin.springfox.bridge.demo.model.TestRequest1;
import org.springframework.stereotype.Service;

@Service
@BridgeApi(value = "TestService3 Apis", description = "测试服务3")
@BridgeGroup("test-group1")
public class TestService3 {
    @BridgeOperation(value = "测试查询", notes = "测试查询方法说明")
    public String testQuery(@BridgeModelProperty(value = "用户id", required = true) long id, @BridgeModelProperty(value = "请求2", required = false) TestRequest1 request){
        return "Test query success, id is " + id;
    }
}

 

    示例中定义了2个分组:test-group1 和 test-group2, 其中TestService1和TestService3归属于test-group1分组,TestService2归属于test-group2分组,其中TestService1中定义了多个不同的方法,在3.4节中会展示这些情况下的多个效果

3.4 示例效果展示

浏览器访问地址:http:${host}:${port}/${server.context-path}/swagger-ui.html

  1. 首先看下面两个分组的截图,其中test-group1:
test-group1.png

test-group2:

test-group2.png

说明:

四、springfox-bridge注解说明

4.1 springfox-bridge自定义注解
注解 位置 主要字段 说明 对标原生注解
BridgeApi description 对类进行说明 io.swagger.annotations.Api
BridgeGroup value 标识分组
BridgeOperation 方法 value、notes value、notes分别标识方法概要和详细说明 io.swagger.annotations.ApiOperation
BridgeModelProperty 入参 value 标识入参说明 io.swagger.annotations.ApiModelProperty
4.2 兼容的springfox swagger原生注解

目前兼容入参的请求体的model用io.swagger包的原生注解@ApiModel@ApiModelProperty ,后续会提供其它支持

五、展望

目前springfox-bridge的1.0.1版本已经发布到maven中央仓库,后续会持续更新,支持更多的特性, 比如: 兼容更多的springfox swagger的原生注解、支持返回体说明、支持author自定义、支持入参类型全名展示等。项目源码见github: https://github.com/double-bin/springfox-bridge

上一篇下一篇

猜你喜欢

热点阅读