Dataway

2020-07-04

快速创建接口神器Dataway!

Dataway与Spring Boot整合: https://www.hasor.net/web/dataql/dataway/for_boot.html

Hasor文档 http://www.hasor.net/doc/

配置

多数据源配置

/**
 * 类说明: 数据库配置
 *
 * @author : Malu
 * @Since : 2020-08-25 16:58
 **/
@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.druid.ds1")
    public DataSource ds1() {
        return new DruidDataSource();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.ds2")
    public DataSource ds2() {
        return new DruidDataSource();
    }
}

dataway配置

/**
 * 类说明: dataway配置文件
 *
 * @author : Malu
 * @Since : 2020-08-26 09:36
 **/
@DimModule
@Component
public class DatawayConfig implements SpringModule {

    @Resource(name = "ds1")
    private DataSource dataSource = null;

    @Resource(name = "ds2")
    private DataSource dataSource2 = null;

    @Override
    public void loadModule(ApiBinder apiBinder) throws Throwable {
        // .DataSource form Spring boot into Hasor
        apiBinder.installModule(new JdbcModule(Level.Full, this.dataSource));
        apiBinder.installModule(new JdbcModule(Level.Full, "ds2", this.dataSource2));

        apiBinder.bindSpiListener(ResultProcessChainSpi.class, new ResultProcessChainSpi() {
            public Object callAfter(boolean formPre, ApiInfo apiInfo, Object result) {

                Map<String, Object> optionMap = apiInfo.getOptionMap();
                optionMap.put("resultStructure", false); // 批量关掉 Structure
                apiInfo.setOptionMap(optionMap);

                if (formPre) {
                    return result; //如果前置拦截器处理了。那么后置拦截器就不处理。
                }
                return new HashMap<String, Object>() ;
            }

            public Object callError(boolean formPre, ApiInfo apiInfo, Throwable e) {
                return new HashMap<String, Object>() ;
            }
        });
    }
}

application.yml

server:
  port: 11123
spring:
  datasource:
    druid:
      ds1:
        name: "localhost"
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/dataway?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
        username: root
        password:
      ds2:
        name: "localhost2"
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/dataway2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
        username: root
        password:
# 是否启用 Dataway 功能(必选:默认false)
HASOR_DATAQL_DATAWAY: true
# 是否开启 Dataway 后台管理界面(必选:默认false)
HASOR_DATAQL_DATAWAY_ADMIN: true

Spring Boot入口配置

@EnableHasor()
@EnableHasorWeb()
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Dataway {

    public static void main(String[] args) {
        SpringApplication.run(Dataway.class, args);
    }

}

Swagger配置

pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

SwaggerProvider.java

/**
 * 类说明: Swagger3提供者
 *
 * @author : Malu
 * @Since : 2020-09-01 10:16
 **/
@Component
@Primary
public class SwaggerProvider implements SwaggerResourcesProvider {

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        resources.add(swaggerResource("Dataway接口", "/interface-ui/api/docs/swagger2.json", "1.0"));
        resources.add(swaggerResource("应用接口", "/v3/api-docs", "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;
    }
}

SwaggerConfig.java

/**
 * 类说明: Swagger3配置文件
 *
 * @author : Malu
 * @Since : 2020-08-31 17:49
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bric.datacloud.dataway.controller"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("应用接口")
                .description("欢迎访问")
                .contact(new Contact("malu", "http://malu.me", "malu@malu.me"))
                .version("1.0")
                .build();
    }
}