Java interface configuration framework magic API

Keywords: architecture

1. magic three frameworks

magic-api
Interface rapid development framework
For the Java based interface rapid development framework, the interface will be written through the UI interface provided by magic API, which will be automatically mapped to HTTP interface. Common HTTP API interface development can be completed without defining Java objects such as Controller, Service, Dao, Mapper, XML, VO, etc.

magic-page-designer
Online page rapid development platform
The online page rapid development platform based on vue mainly changes all vue codes to online configuration, and finally obtains a page in json format

spider-flow
New generation crawler platform
The new generation crawler platform based on Java defines the crawler process graphically, and the crawler can be completed without writing code.

2. Magic API overview

2.2 introduction to magic API

Today, we give you an excellent low code platform for Amway, which can realize the dynamic configuration and release of the interface without writing code.

Magic API is a rapid interface development framework based on Java. The interface will be written through the UI interface provided by magic API and automatically mapped to HTTP interface. Common HTTP API interface development can be completed without defining Java objects such as Controller, Service, Dao, Mapper, XML and VO.

The above is the official definition. In short, before we developed an interface, we need at least Controller, Service, Dao and Mapper files, as well as various configurations, build and start the springboot project. After publishing, the interface can be accessed and the interface can be completed.
What magic needs to do is to dynamically configure the interface path through the UI interface, access parameters, click save, and the interface can be accessed after publishing. It does not need to write code, but only needs simple configuration. It also supports database operations such as things, paging and http requests, which is a little similar to the current low code platform, For some non core dictionaries and other static data, it is very convenient to use. It is a very excellent low code framework that can quickly develop interfaces.

Official website address: https://www.ssssssss.org/
Document address: https://www.ssssssss.org/magic-api/
Demo address: https://magic-api.ssssssss.org/

2.2 magic API features

characteristic

support MySQL,MariaDB,Oracle,DB2,PostgreSQL,SQLServer Such support jdbc Canonical database
 Support non relational database Redis,Mongodb
 Support cluster deployment and automatic interface synchronization.
Support paging query and user-defined paging query
 Support multi data source configuration and online data source configuration
 support SQL Caching, and customization SQL cache
 Support customization JSON Results, custom paging results
 Support interface permission configuration, interceptor and other functions
 Supports dynamic modification of data sources at run time
 support Swagger Interface document generation
 be based on magic-script Script engine, dynamic compilation, no restart, real-time release
 support Linq Simple query, association and transformation
 Support database transactions SQL Support splicing, placeholder, judgment and other syntax
 Support file upload, download and image output
 Support script historical version comparison and recovery
 Support script code automatic prompt, parameter prompt, suspension prompt and error prompt
 Support import Spring Medium Bean,Java Classes in
 Support online debugging
 It supports user-defined operations such as user-defined tool classes, user-defined module packages, user-defined type extensions, user-defined dialects, and user-defined column name conversions

3. Magic API framework construction

3.1 creating database

CREATE TABLE `magic_api_file` (
  `file_path` varchar(512) NOT NULL,
  `file_content` mediumtext,
  PRIMARY KEY (`file_path`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

CREATE TABLE `magic_api_backup` (
  `id` varchar(32) NOT NULL COMMENT 'Original object ID',
  `create_date` bigint(13) NOT NULL COMMENT 'Backup time',
  `tag` varchar(32) DEFAULT NULL COMMENT 'label',
  `type` varchar(32) DEFAULT NULL COMMENT 'type',
  `name` varchar(64) DEFAULT NULL COMMENT 'old name',
  `content` mediumtext COMMENT 'Backup content',
  `create_by` varchar(64) DEFAULT NULL COMMENT 'Operator',
  PRIMARY KEY (`id`,`create_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

select * from magic_api_info;
CREATE TABLE `magic_api_info` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID Primary key',
  `name` varchar(64) DEFAULT NULL COMMENT 'magic name',
  `address` varchar(64) DEFAULT NULL COMMENT 'api address',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='Magic api';
-- insert data v
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (14, 'authentication  API', '/auth/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (15, 'user API', '/user/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (16, 'commodity API', '/goods/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (17, 'Category API', '/category/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (18, 'order API', '/order/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (19, 'activity API-NEW', '/activity/**');
INSERT INTO `magic`.`magic_api_info`(`id`, `name`, `address`) VALUES (20, 'Marketing API', '/marking/**');

3.2 creating a springboot project

3.3 introducing dependencies

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- with spring-boot-starter Reference by -->
        <dependency>
            <groupId>org.ssssssss</groupId>
            <artifactId>magic-api-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>

3.4 application.yml configuration

server:
  port: 9999
  # Configure static resources and enable gzip compression
  compression:
    enabled: true
    min-response-size: 128
# Configure master data source
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  datasource:
    url: jdbc:mysql://localhost:3306/magic?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
#  data:
    # Configure mongo data source
#    mongodb:
#      host: localhost
#      port: 27017
#      database: magicapi
#      username: magicapi
#      password: 123456
  # Configure redis data source
#  redis:
#    host: localhost
#    port: 6379
#    database: 4
#    timeout: 5000
logging:
  level:
    org:
      springframework:
        jdbc:
          core:
            JdbcTemplate: DEBUG #Print SQL
            StatementCreatorUtils: TRACE  #Print SQL parameters
# magic configuration
magic-api:
  web: /magic/web
  resource:
    type: database  # Configure the interface storage method. Select save in database here
    table-name: magic_api_file  # Table name in database
    prefix: /magic-api  # prefix
#    location: classpath:magic-api
# Refer to for other configurations https://ssssssss.org/config/

  # Permission configuration
  security-config:
    username: admin # User name for login
    password: 123456 # Password for login

  # Response code configuration
  response-code-config:
    success: 200 #code value of successful execution
    invalid: 400 #code value that failed parameter validation
    exception: 500 #Execute code value with exception

  # swagger configuration
  swagger-config:
    version: 1.0
    description: MagicAPI Interface information
    title: MagicAPI Swagger Docs
    name: MagicAPI Interface
    location: /v2/api-docs/magic-api/swagger2.json

3.5 startup

MagicApiApplication

package com.zrj.magic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * magic-api
 *
 * @author zrj
 * @since 20210910
 */
@EnableSwagger2
@SpringBootApplication
public class MagicApiApplication {

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

}

3.6 other custom classes

The rest are customized configuration classes. See magic API simple for details.
gitee address: https://gitee.com/ssssssss-team/magic-api-example

Magic API configuration class MagicAPIConfiguration

package com.zrj.magic.configuration;

import com.zrj.magic.interceptor.CustomRequestInterceptor;
import com.zrj.magic.interceptor.CustomUIAuthorizationInterceptor;
import com.zrj.magic.provider.*;
import com.zrj.magic.scripts.CustomFunction;
import com.zrj.magic.scripts.CustomFunctionExtension;
import com.zrj.magic.scripts.CustomModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.ssssssss.magicapi.adapter.Resource;
import org.ssssssss.magicapi.config.MagicDynamicDataSource;
import org.ssssssss.magicapi.provider.GroupServiceProvider;
import org.ssssssss.magicapi.provider.PageProvider;

import javax.sql.DataSource;

/**
 * magic-api Configuration class
 * Only multiple data sources are configured below
 * If necessary, you can release the / / @ Bean annotation to see the effect
 */
@Configuration
public class MagicAPIConfiguration {

    /**
     * Configure multiple data sources
     *
     * @see MagicDynamicDataSource
     */
    @Bean
    public MagicDynamicDataSource magicDynamicDataSource(DataSource dataSource) {
        MagicDynamicDataSource dynamicDataSource = new MagicDynamicDataSource();
        dynamicDataSource.setDefault(dataSource); // Set default data source
        dynamicDataSource.add("slave", dataSource);
        return dynamicDataSource;
    }

    /**
     * Configure custom JSON results
     */
    //@Bean
    public CustomJsonValueProvider customJsonValueProvider() {
        return new CustomJsonValueProvider();
    }

    /**
     * Configure paging access method
     */
    // @Bean
    public PageProvider pageProvider() {
        return new CustomPageProvider();
    }

    /**
     * User defined UI authentication
     */
    //@Bean
    public CustomUIAuthorizationInterceptor customUIAuthorizationInterceptor() {
        return new CustomUIAuthorizationInterceptor();
    }

    /**
     * Custom request interceptor (authentication)
     */
    // @Bean
    public CustomRequestInterceptor customRequestInterceptor() {
        return new CustomRequestInterceptor();
    }

    /**
     * Custom SQL cache
     */
    // @Bean
    public CustomSqlCache customSqlCache() {
        return new CustomSqlCache();
    }

    /**
     * Custom function
     */
    // @Bean
    public CustomFunction customFunction() {
        return new CustomFunction();
    }

    /**
     * Custom method extension
     */
    // @Bean
    public CustomFunctionExtension customFunctionExtension() {
        return new CustomFunctionExtension();
    }

    /**
     * Custom module
     */
    // @Bean
    public CustomModule customModule() {
        return new CustomModule();
    }

    /**
     * Custom scripting language
     */
    // @Bean
    public CustomLanguageProvider customLanguageProvider() {
        return new CustomLanguageProvider();
    }

    /**
     * Custom column name conversion
     */
    // @Bean
    public CustomMapperProvider customMapperProvider() {
        return new CustomMapperProvider();
    }

    /**
     * User defined interface script encryption and decryption
     */
    // @Bean
    public CustomApiServiceProvider customApiServiceProvider(Resource resource, GroupServiceProvider groupServiceProvider) {
        return new CustomApiServiceProvider(resource, groupServiceProvider);
    }

}

4. magic implementation verification

4.1 magic ui

Access address: http://localhost:9999/magic/web/index.html
Start console output

********************************************Current service related address********************************************
Service started successfully, magic-api Built in boot! Access URLs:
Interface local address: 		http://localhost:9999/
Interface external access address: 	http://10.255.1.46:9999/
Interface configuration platform: 		http://10.255.1.46:9999/magic/web/index.html
 Output can be turned off by configuration: 	magic-api.show-url=false
********************************************Current service related address********************************************

4.2 swagger

Swagger address: http://localhost:9999/swagger-ui.html#/
This needs annotation @ EnableSwagger2, otherwise it cannot be found.

Posted by eyespark on Sat, 11 Sep 2021 18:24:35 -0700