Interface rapid development framework magic API 2 X initial experience

Posted by smashmouth on Mon, 28 Feb 2022 12:19:36 +0100

What is magic API?

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.

For details, see: Magic API Guide

The latest version is: 2.0.0-beta 1, but due to 2 X reconstructed the underlying storage logic, 1 X cannot be upgraded directly to 2 Version x [when upgrading, all interfaces, functions and data sources in 1.x need to be exported first, and then imported into 2.x]

First experience

Import example into idea

  • Clone project git clone https://gitee.com/ssssssss-team/magic-api-example.git
  • Import into idea
  • Modify the configuration resources / application yml
  • Create table in database: magic_api_file_v2,test_data and insert initialization data
  • Start MagicAPIExampleApplication, the spring boot application
  • Access via WEB browser: http://localhost:9999/magic/web/index.html

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://172.25.21.188:3306/test?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
    username: videoweb
    password: suntek
magic-api:
  web: /magic/web
  resource:
    type: database  # Configure the interface storage method. Select save in database here
    table-name: magic_api_file_v2  # Table name in database
    prefix: /magic-api  # prefix
  swagger:
    version: 1.0
    description: MagicAPI Interface information
    title: MagicAPI Swagger Docs
    name: MagicAPI Interface
    location: /v2/api-docs/magic-api/swagger2.json

Creating tables and initializing data SQL statements

The following statements are executed in the MySQL data source configured above.

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

create table test_data
(
    id   bigint       not null
        primary key,
    name varchar(100) null
);
INSERT INTO test_data (id, name) VALUES (1, 'magicApi');
INSERT INTO test_data (id, name) VALUES (2, 'xiaoDong');

API management interface

API management interface is as follows:

Configure interface API

Query interface configuration

  • Create a group and specify the group name and path
  • Create an interface and specify the request method, interface name, interface path and request parameter information (for the setting method, see: Ask for parameter acquisition ). interface description.
  • After the configuration is completed, you can click the run button in the upper right corner to test

Add interface configuration

According to the requirements of different business scenarios, the following wording is selected:

  • return db.table('test_data').insert({ id : 4, name : 'admin'})
  • return db.table('test_data').primary('id').save({ id : 4, name : 'admin'})

Update interface configuration

return db.table('test_data').primary('id').update({ id: 4, name : 'Zhang Sanfeng'})

Delete interface configuration

return db.table('test_data').primary('id').where().eq('id',4).delete()

View swagger information

Access address: http://localhost:9999/swagger-ui.html

Pit filling record

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerEx

In the magic API quick start document, it is required to add: the version of spring boot starter parent is 2.6.4, but the swagger version used in example is 2.9.2. Because the springboot version is too high and lacks the environment required for swagger operation [what is missing is unknown], reduce the version of springboot to 2.4.5 to start normally.

The specific error information is as follows:

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.16.jar:5.3.16]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.16.jar:5.3.16]
......
......
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
	at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356) ~[na:1.8.0_131]

Topics: Spring Boot microservice