Dataway介紹
Dataway 是基於 DataQL 服務聚合能力,為應用提供的一個接口配置工具。使得使用者無需開發任何代碼就配置一個滿足需求的接口。 整個接口配置、測試、冒煙、發布。一站式都通過 Dataway 提供的 UI 界面完成。UI 會以 Jar 包方式提供並集成到應用中並和應用共享同一個 http 端口,應用無需單獨為 Dataway 開辟新的管理端口。
這種內嵌集成方式模式的優點是,可以使得大部分老項目都可以在無侵入的情況下直接應用 Dataway。進而改進老項目的迭代效率,大大減少企業項目研發成本。
Dataway 工具化的提供 DataQL 配置能力。這種研發模式的變革使得,相當多的需求開發場景只需要配置即可完成交付。 從而避免了從數據存取到前端接口之間的一系列開發任務,例如:Mapper、BO、VO、DO、DAO、Service、Controller 統統不在需要。
研發管理中對於開發的 API 通常需要統一管理,在這一令領域比較常用的是 Swagger。它可以通過 注解方式直接將開發的真實 API 形象的進行文檔化,並且提供了一個 Swagger-UI 的界面來查閱。除此之外 Swagger-UI 上還可以發起模擬調用,這一功能是在是讓開發人員非常舒心。
Dataway 的優勢是在於,一些簡單的接口或者聚合服務都可以不在需要開發。用過 Interface-UI 就可以配置和管理。在dataway 4.1.8 版本之前,研發管理上開發者需要在兩個 UI 上切換來實現接口的測試和查閱。
新的 4.1.8 發布之后補充了這一短板,讓 Dataway 上配置的接口可以直接產生 Swagger2 的API 文檔。進一步的開發者可以將這個文檔整合到應用自身的 API 文檔中。統一管理和查閱、使用。
進一步的利用 Swagger 還可以通過 postman 來統一接口的調試、利用 yapi 還可以對接口進行批量的管理和驗證。
接下來本文就會引導讀者如何在一個 Spring 項目中讓 Dataway 和 Swagger 整合起來。
第一步:引入Swagger
整合多個 Swagger 文檔需要較高 Swagger 版本的支持,我們這里選用 2.7.0。
<!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
第二步:引入Dataway
在 4.1.8 版本中 Dataway 支持了 Swagger 的文檔輸出,我們選用它。
<dependency> <groupId>net.hasor</groupId> <artifactId>hasor-spring</artifactId> <version>4.1.8</version> </dependency> <dependency> <groupId>net.hasor</groupId> <artifactId>hasor-dataway</artifactId> <version>4.1.8</version> </dependency>
第三步:配置 Dataway 讓其可以工作
@DimModule @Component public class ExampleModule implements SpringModule { @Autowired private DataSource dataSource = null; // 應用自己的數據源 @Override public void loadModule(ApiBinder apiBinder) throws Throwable { // .DataSource form Spring boot into Hasor apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource)); // 初始化Dataway 的數據源 // .custom DataQL // } @Override public void onStart(AppContext appContext) throws Throwable { // } }
更詳細步驟可以參考《絕了!Dataway讓Spring Boot不再需要Controller、Service、DAO、Mapper》https://my.oschina.net/ta8210/blog/3234639
第四步:整合 Dataway 的 Swagger 文檔到一起
首先新建一個類,在應用中啟用 Swagger
@EnableSwagger2 @Configuration() public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2)// .apiInfo(apiInfo())// .select()// .apis(RequestHandlerSelectors.basePackage("net.example.hasor"))// .paths(PathSelectors.any())// .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder()// .title("Spring Boot中使用Swagger2構建RESTful APIs")// .description("歡迎到我的GitHub:https://github.com/1610wang/")// .termsOfServiceUrl("https://github.com/1610wang/")// .contact("wxy").version("1.0")// .build(); } }
其次利用 Swagger 的接口整合兩個文檔到一起
@Component @Primary public class SwaggerProvider implements SwaggerResourcesProvider { @Override public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); resources.add(swaggerResource("應用接口", "/v2/api-docs", "1.0")); resources.add(swaggerResource("Dataway接口", "/interface-ui/api/docs/swagger2.json", "1.0")); return resources; } private SwaggerResource swaggerResource(String name, String location, String version) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion(version); return swaggerResource; } }
最后啟動應用,首先輸入 Swagger-UI 看到的是應用自身的 API 信息。由於我們是一個空項目,這里沒有任何顯示。
接着我們切換 Swagger 的文檔
展開 Default 就可以看到我們配置的接口了。
在切換到 Dataway 的UI 中對比一下是不是所有已經發布的 API 都已經在 Swagger 中展示出來了。
第五步:用 SwaggerUI 測試Dataway 接口
點開其中一個 Dataway 接口我們嘗試輸入必要的參數測試一下。
返回結果
注意事項
這是一項新的功能,對於 4.1.8 之前已經在使用的老接口。您需要重新發布一次接口。
否則的話,Dataway 並沒有記錄接口的入參和出參格式,因此也就無法生成一個准確的 Swagger 的接口入參和出參信息。