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-starterdependency to your
pom.xml:
<code><dependency>
<groupId>com.geekhalo.lego</groupId>
<artifactId>lego-starter</artifactId>
<version>0.1.11-rest-SNAPSHOT</version>
</dependency></code>Add Swagger dependencies:
<code><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></code>Create a Swagger configuration class:
<code>@Configuration
@EnableSwagger2
public class SpringFoxConfiguration {
}</code>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:
<code>@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);
}</code>The Swagger UI now shows endpoints for
cancel,
create, and
paySuccesswith the same signatures as manually written Controllers.
2.4 Query Controller
Similarly, annotate a QueryService:
<code>@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);
}</code>The generated Query controller mirrors the service methods, providing clear request and response structures.
3. Design & Extension
3.1 Unified Controller
The
QueryDispatcherControlleracts 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
serviceNameand
methodNameand invokes it.
3.2 Swagger Integration
The
QueryServicesProviderextracts method metadata from
QueryMethodRegistry, creates request handlers, and registers them with Swagger, enabling dynamic API documentation.
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.