Accelerate Spring Boot API Development with magic-api: A Hands‑On Guide
This tutorial shows how Java developers can use the magic‑api framework to rapidly create Spring Boot CRUD APIs without writing Controllers, Services, or DAOs, covering Maven setup, configuration, database schema, code‑free endpoint design, validation, transactions, Swagger integration, and deployment.
magic-api Introduction
magic-api is a Java‑based rapid API development framework that lets you create HTTP interfaces through its UI without defining Controller, Service, Dao, Mapper, XML, or VO objects.
Usage
Below we demonstrate how to use magic‑api to develop API interfaces.
Using in SpringBoot
magic‑api natively supports SpringBoot and integrates seamlessly.
First, add the magic‑api dependency to
pom.xml:
<code><!-- Interface rapid development framework magic‑api -->
<dependency>
<groupId>org.ssssssss</groupId>
<artifactId>magic-api-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency></code>Then configure the data source and magic‑api settings in
application.yml:
<code>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: 10</code>Create the required tables in MySQL:
<code>CREATE TABLE `magic_api_file` (
`id` bigint(255) NOT NULL AUTO_INCREMENT,
`file_path` varchar(255) DEFAULT NULL,
`file_content` text,
PRIMARY KEY (`id`)
);
CREATE TABLE `pms_brand` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`big_pic` varchar(255) DEFAULT NULL,
`brand_story` varchar(255) DEFAULT NULL,
`factory_status` bit(1) DEFAULT NULL,
`first_letter` varchar(255) DEFAULT NULL,
`logo` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`product_comment_count` int(11) DEFAULT NULL,
`product_count` int(11) DEFAULT NULL,
`show_status` bit(1) DEFAULT NULL,
`sort` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;</code>Start the project and access the magic‑api UI at
http://localhost:8080/magic/web.
CRUD Operations
Using the UI and magic‑script , we can create endpoints for create, read, update, delete, and pagination.
Create a brand (POST
/create) with body parameters:
<code>// Access request body directly
return db.table('pms_brand').insert(body);</code>Read by ID (GET
/detail/{id}) using path variable:
<code>return db.table('pms_brand')
.where().eq('id', path.id)
.selectOne();</code>Update (POST
/update) with body:
<code>return db.table('pms_brand')
.primary('id', body.id)
.update(body);</code>Paginated query (GET
/page) – pagination parameters are already configured in
application.yml:
<code>return db.table('pms_brand').page();</code>Delete (POST
/delete/{id}) using path variable:
<code>return db.update('delete from pms_brand where id=#{id}');</code>Parameter Validation
Use the assert module to validate request parameters.
<code>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);</code>Result Mapping
Transform query results with map to return custom fields.
<code>var list = db.table('pms_brand').select();
return list.map(item => ({
name: item.name,
firstLetter: item.firstLetter,
showStatus: item.showStatus ? 'Not displayed' : 'Displayed'
}));</code>Transaction Support
Wrap operations in db.transaction() for automatic or manual transaction handling.
<code>import assert;
var val = 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 v2;
});
return val;</code>Swagger Integration
Integrate Swagger UI by adding Swagger dependencies to pom.xml and configuring magic-api in application.yml .
Add Swagger dependencies:
<code><dependencies>
<!-- Swagger UI API documentation tool -->
<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></code>Configure Swagger in
application.yml:
<code>magic-api:
swagger-config:
name: MagicAPI Test Interface
title: MagicAPI Swagger Docs
description: MagicAPI test interface information
version: 1.0
location: /v2/api-docs/magic-api/swagger2.json</code>Visit
http://localhost:8080/swagger-ui.htmlto view the generated API documentation.
Conclusion
magic‑api is an interesting framework that enables rapid API development through simple scripts in its UI, but as a niche tool it still has room to grow.
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.