Accelerate Java API Development with magic‑api: No Controllers Needed
This tutorial shows how to use the Java‑based magic‑api framework to generate Spring Boot HTTP endpoints directly from a UI, covering dependency setup, datasource configuration, database preparation, CRUD scripts, validation, result mapping, transaction handling, and Swagger integration.
What is magic‑api
magic‑api is a Java‑based rapid API development framework that maps UI‑defined scripts to HTTP endpoints, removing the need to write Controller, Service, DAO, Mapper, XML, or VO classes.
Getting Started
Integration with Spring Boot
Add the starter dependency to pom.xml:
<dependency>
<groupId>org.ssssssss</groupId>
<artifactId>magic-api-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>Configure the 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: 10Database Preparation
Create the table that stores API definitions and a sample business table:
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;CRUD Example – Brand Management
Insert a brand (script for the add endpoint):
// body contains the request payload
return db.table('pms_brand').insert(body);Configure the endpoint as POST /create with request body parameters.
Detail endpoint (retrieve a brand by ID):
return db.table('pms_brand')
.where().eq('id', path.id)
.selectOne();Configure as GET /detail/{id}.
Update endpoint:
return db.table('pms_brand')
.primary('id', body.id)
.update(body);Configure as POST /update.
Paginated list endpoint: return db.table('pms_brand').page(); Configure as GET /page (pagination parameters are read from application.yml).
Delete endpoint (implemented with an update statement):
return db.update('delete from pms_brand where id=#{id}');Configure as POST /delete/{id}.
Parameter Validation
Import the assert module and add checks, for example:
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:
var list = db.table('pms_brand').select();
return list.map(item => ({
name: item.name,
firstLetter: item.firstLetter,
showStatus: item.showStatus ? 'Not displayed' : 'Displayed'
}));Transaction Support
Wrap multiple operations in a transaction:
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;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>Swagger configuration 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.jsonAccess the generated documentation at http://localhost:8080/swagger-ui.html.
Project Repository
https://gitee.com/ssssssss-team/magic-api
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.
