Eliminate Manual Controllers: Auto‑Expose Command & Query Services with Lego‑Starter
This guide shows how to eliminate manual Controller code by automatically exposing CommandService and QueryService as web endpoints, integrating with Swagger for dynamic API documentation, and explains the underlying unified controller architecture and extension points.
1. Overview
In daily development, writing Controllers is tedious; many companies forbid business logic in Controllers, yet source code often contains it. The author argues that Controllers are unnecessary.
1.1 Background
By encapsulating CommandService and QueryService, the author built application services, then wrote Controllers to expose them, which is repetitive. The strategy is to let the framework handle this repetitive work.
1.2 Goal
The goal is to avoid writing Controllers while preserving their effect.
Expose CommandService and QueryService directly as web endpoints without writing Controller code.
Integrate with Swagger to dynamically generate API documentation for front‑end consumption.
2. Quick Start
2.1 Environment Preparation
Add the
lego-starterdependency to the Maven
pom.xml:
<code><dependency>
<groupId>com.geekhalo.lego</groupId>
<artifactId>lego-starter</artifactId>
<version>0.1.11-rest-SNAPSHOT</version>
</dependency></code>Add Swagger related 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
SpringFoxConfigurationclass to enable Swagger:
<code>@Configuration
@EnableSwagger2
public class SpringFoxConfiguration {
}
</code>2.2 First Look at Unified Controllers
Open
http://127.0.0.1:8080/swagger-ui/and you will see two generated Controllers:
command-dispatcher-controller : exposes
CommandServicefor write/update operations, supporting both RequestBody and RequestParam.
query-dispatcher-controller : exposes
QueryServicefor read operations, also supporting RequestBody and RequestParam, with additional support for HTTP GET.
2.3 Command Controller
2.3.1 Enable Command Controller
Annotate the
OrderCommandServiceinterface with
@AutoRegisterWebControllerto expose it as a web endpoint:
<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>2.3.2 Use Order Command Controller
Visit the Swagger UI again; a new group of endpoints appears for the command methods.
All methods defined in
OrderCommandServiceare automatically exposed as REST endpoints, with both body‑based and param‑based variants.
2.4 Query Controller
2.4.1 Enable Query Controller
Annotate the
OrderQueryServiceinterface similarly:
<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>2.4.2 Use Order Query Controller
After refreshing Swagger UI, another set of endpoints appears for the query methods.
The request/response structures are clear, and the generated endpoints behave the same as hand‑written Controllers.
3. Design & Extension
The design consists of two parts:
Provide a unified Controller that acts as a dispatcher for all requests.
Integrate a plugin with Swagger to generate complete API documentation.
3.1 Unified Controller
The
QueryDispatcherControllerserves as the entry point for all query requests. The core architecture is illustrated below:
Initialization flow:
Spring instantiates all
QueryServicebeans.
Each instance is automatically registered in
QueryServicesRegistry.
QueryMethodRegistryparses methods from the registry and registers them.
Execution flow:
The client sends a request.
The request is routed to the appropriate method of
QueryDispatcherController.
The controller retrieves the corresponding
QueryMethodfrom
QueryMethodRegistryusing
serviceNameand
methodName, invokes the business method, and returns the result.
3.2 Swagger Integration
QueryServicesProviderworks with Swagger to generate full API docs. The overall design is shown below:
Core process:
QueryServicesProviderobtains
QueryMethodinformation from
QueryMethodRegistry.
It creates
RequestHandlerobjects and registers them with Swagger.
When a user accesses Swagger UI, the handlers are converted into API documentation.
4. Project Information
Repository: https://gitee.com/litao851025/lego
Documentation: https://gitee.com/litao851025/lego/wikis/support/web
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.