Rapid API Development with magic‑api: No Controllers, No Boilerplate
This guide shows how to use the Java‑based magic‑api framework to generate CRUD REST endpoints directly from a UI, covering Maven setup, datasource configuration, script‑based request handling, validation, result mapping, transactions, and Swagger integration.
Overview
magic-apiis a Java‑based rapid API development framework. It provides a web UI for defining API scripts that are automatically exposed as HTTP endpoints, eliminating the need for explicit Controllers, Services, DAOs, Mappers, XML or VO classes.
Integration with Spring Boot
Add the starter dependency (version 1.0.2) to pom.xml:
<dependency>
<groupId>org.ssssssss</groupId>
<artifactId>magic-api-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>Configure datasource and magic-api settings in application.yml:
spring:
datasource:
url: jdbc:mysql://localhost:3306/magic_api?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
magic-api:
web: /magic/web
resource:
type: database
tableName: magic_api_file
prefix: /magic-api
readonly: false
sql-column-case: camel
page-config:
size: size
page: page
default-page: 1
default-size: 10Create the required tables in MySQL:
CREATE TABLE `magic_api_file` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`file_path` VARCHAR(255),
`file_content` TEXT,
PRIMARY KEY (`id`)
);
CREATE TABLE `pms_brand` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`big_pic` VARCHAR(255),
`brand_story` VARCHAR(255),
`factory_status` BIT,
`first_letter` VARCHAR(255),
`logo` VARCHAR(255),
`name` VARCHAR(255),
`product_comment_count` INT,
`product_count` INT,
`show_status` BIT,
`sort` INT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;Run the Spring Boot application and open http://localhost:8080/magic/web to access the UI.
CRUD Example – Brand Management
Insert (POST /create)
// body contains the request payload
return db.table('pms_brand').insert(body);Query by ID (GET /detail/{id})
return db.table('pms_brand')
.where()
.eq('id', path.id)
.selectOne();Update (POST /update)
return db.table('pms_brand')
.primary('id', body.id)
.update(body);Paginated list (GET /page) return db.table('pms_brand').page(); Delete (POST /delete/{id})
return db.update('delete from pms_brand where id=#{id}');Parameter Validation
Import the assert module and use its methods to validate request data. Validation failures abort execution and return the specified HTTP status and message.
import assert;
assert.notEmpty(body.name, 400, 'Name cannot be empty!');
assert.notEmpty(body.firstLetter, 400, 'First letter cannot be empty!');
return db.table('pms_brand').insert(body);Result Transformation
Map query results to a custom structure before returning:
var list = db.table('pms_brand').select();
return list.map(item => ({
name: item.name,
firstLetter: item.firstLetter,
showStatus: item.showStatus ? 'Not Display' : 'Display'
}));Transaction Support
Wrap multiple database operations in a transaction using db.transaction():
import assert;
var result = db.transaction(() => {
var exist = db.table('pms_brand')
.where().eq('id', body.id)
.selectOne();
assert.notNull(exist, 404, 'Brand not found!');
db.table('pms_brand').primary('id', body.id).update(body);
return 'ok';
});
return result;Swagger Integration
Add Swagger dependencies to pom.xml:
<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>Configure Swagger in application.yml:
magic-api:
swagger-config:
name: MagicAPI Test API
title: MagicAPI Swagger Docs
description: MagicAPI test interface information
version: 1.0
location: /v2/api-docs/magic-api/swagger2.jsonSwagger UI is available at http://localhost:8080/swagger-ui.html.
Project Repositories
Gitee: https://gitee.com/ssssssss-team/magic-api
GitHub: https://github.com/macrozheng/mall-learning/tree/dev-v2/mall-tiny-magic-api
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
