Skip Manual Controllers: Auto‑Generate SpringBoot APIs with Lego‑Starter
This guide shows how to eliminate manual SpringBoot Controllers by using the Lego‑Starter to automatically expose CommandService and QueryService as web endpoints, integrate with Swagger for dynamic API documentation, and provides step‑by‑step setup, code examples, and architectural design for unified controller handling.
1. Overview
In daily development, writing Controllers is tedious. Many companies enforce that Controllers contain no business logic, only parameter parsing and result conversion. However, source code often contains unwanted logic. This article proposes eliminating Controllers entirely by exposing CommandService and QueryService directly as web endpoints.
1.1 Background
By encapsulating CommandService and QueryService as application services, we can expose capabilities via Controllers, which is repetitive and low‑value work. The strategy is to let the framework handle this repetition.
1.2 Goal
Goal: avoid writing Controller code while preserving the same functionality, and integrate with Swagger for dynamic API documentation.
Expose CommandService and QueryService as web interfaces without manual Controllers.
Integrate with Swagger to generate API docs automatically.
2. Quick Start
2.1 Environment Preparation
Add the lego-starter dependency to your pom.xml:
<dependency>
<groupId>com.geekhalo.lego</groupId>
<artifactId>lego-starter</artifactId>
<version>0.1.11-rest-SNAPSHOT</version>
</dependency>Add Swagger dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>3.0.0</version>
</dependency>Create a Swagger configuration class:
@Configuration
@EnableSwagger2
public class SpringFoxConfiguration {
}2.2 First Unified Controller
After starting the application, open http://127.0.0.1:8080/swagger-ui/. Two auto‑generated Controllers appear: command-dispatcher-controller (exposes CommandService) and query-dispatcher-controller (exposes QueryService). Both support RequestBody (JSON) and RequestParam (simple parameters). The Query controller additionally supports HTTP GET.
2.3 Command Controller
Annotate a CommandService interface with @AutoRegisterWebController(name="order") to expose its methods:
@CommandServiceDefinition(domainClass = Order.class, idClass = Long.class, repositoryClass = OrderRepository.class)
@AutoRegisterWebController(name = "order")
public interface OrderCommandService {
void cancel(Long orderId);
Long create(CreateOrderCommand command);
void paySuccess(PaySuccessCommand command);
}The Swagger UI now shows endpoints for cancel, create, and paySuccess with the same signatures as manually written Controllers.
2.4 Query Controller
Similarly, annotate a QueryService:
@QueryServiceDefinition(domainClass = Order.class, repositoryClass = OrderQueryRepository.class)
@Validated
@AutoRegisterWebController(name = "order")
public interface OrderQueryService {
OrderDetail getById(@Valid @NotNull(message = "订单号不能为null") Long id);
Page<OrderDetail> pageByUserId(@Valid @NotNull(message = "查询参数不能为 null") PageByUserId query);
List<OrderDetail> getByUserId(@Valid @NotNull(message = "查询参数不能为 null") GetByUserId getByUserId);
Long countByUser(@Valid @NotNull(message = "查询参数不能为 null") CountByUserId countByUserId);
List<OrderDetail> getPaidByUserId(Long id);
}The generated Query controller mirrors the service methods, providing clear request and response structures.
3. Design & Extension
3.1 Unified Controller
The QueryDispatcherController acts as a universal entry point for all query requests. Initialization registers all QueryService beans into a QueryServicesRegistry, which then populates a QueryMethodRegistry. At runtime, the dispatcher looks up the appropriate method by serviceName and methodName and invokes it.
3.2 Swagger Integration
The QueryServicesProvider extracts method metadata from QueryMethodRegistry, creates request handlers, and registers them with Swagger, enabling dynamic API documentation.
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.
