diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/GlobalResponse.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/GlobalResponse.java index 1c5ae17..539ce90 100644 --- a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/GlobalResponse.java +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/GlobalResponse.java @@ -1,5 +1,6 @@ package com.lanyuanxiaoyao.service.template.common.controller; +import java.util.List; import java.util.Map; /** @@ -28,7 +29,7 @@ import java.util.Map; * *

@@ -165,6 +166,10 @@ public record GlobalResponse(Integer status, String message, T data) { return responseMapData(Map.of(key, value)); } + public static GlobalResponse> responseListData() { + return responseListData(List.of(), 0); + } + /** * 返回CRUD列表数据的成功响应(Integer类型总数) *

@@ -176,8 +181,8 @@ public record GlobalResponse(Integer status, String message, T data) { * @param total 总记录数 * @return 成功响应对象 */ - public static GlobalResponse> responseCrudData(Iterable data, Integer total) { - return responseCrudData(data, total.longValue()); + public static GlobalResponse> responseListData(Iterable data, Integer total) { + return responseListData(data, total.longValue()); } /** @@ -191,8 +196,8 @@ public record GlobalResponse(Integer status, String message, T data) { * @param total 总记录数 * @return 成功响应对象 */ - public static GlobalResponse> responseCrudData(Iterable data, Long total) { - return responseMapData(Map.of("items", data, "total", total)); + public static GlobalResponse> responseListData(Iterable data, Long total) { + return responseSuccess(new ListItem<>(data, total)); } /** @@ -201,10 +206,16 @@ public record GlobalResponse(Integer status, String message, T data) { * 适用于详情查询,将单条记录封装为Map格式 *

* - * @param detail 详情数据 + * @param data 详情数据 * @return 成功响应对象 */ - public static GlobalResponse> responseDetailData(Object detail) { - return responseMapData("detail", detail); + public static GlobalResponse> responseDetailData(T data) { + return responseSuccess(new DetailItem<>(data)); + } + + public record ListItem(Iterable items, Long total) { + } + + public record DetailItem(T item) { } } diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/QueryController.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/QueryController.java index 2b53984..d512721 100644 --- a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/QueryController.java +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/QueryController.java @@ -1,7 +1,5 @@ package com.lanyuanxiaoyao.service.template.common.controller; -import java.util.Map; - /** * 查询控制器接口,用于定义统一的查询实体详情和列表的接口规范 * @@ -53,9 +51,9 @@ import java.util.Map; * *

* - * @param 查询结果的实体类型 + * @param 查询结果的实体类型 */ -public interface QueryController { +public interface QueryController { String LIST = "/list"; String DETAIL = "/detail/{id}"; @@ -65,7 +63,7 @@ public interface QueryController { * @return GlobalResponse> 返回实体列表 * @throws Exception 查询过程中可能抛出的异常 */ - GlobalResponse> list() throws Exception; + GlobalResponse> list() throws Exception; /** * 根据查询条件获取实体列表 @@ -74,7 +72,7 @@ public interface QueryController { * @return GlobalResponse> 返回符合条件的实体列表 * @throws Exception 查询过程中可能抛出的异常 */ - GlobalResponse> list(Query query) throws Exception; + GlobalResponse> list(Query query) throws Exception; /** * 根据ID获取实体详情 @@ -83,5 +81,5 @@ public interface QueryController { * @return GlobalResponse 返回实体详情 * @throws Exception 查询过程中可能抛出的异常 */ - GlobalResponse detail(Long id) throws Exception; + GlobalResponse detail(Long id) throws Exception; } \ No newline at end of file diff --git a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/SimpleController.java b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/SimpleController.java index eb159fb..8ddf1d8 100644 --- a/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/SimpleController.java +++ b/spring-boot-service-template-common/src/main/java/com/lanyuanxiaoyao/service/template/common/controller/SimpleController.java @@ -1,4 +1,4 @@ package com.lanyuanxiaoyao.service.template.common.controller; -public interface SimpleController extends SaveController, QueryController, RemoveController { +public interface SimpleController extends SaveController, QueryController, RemoveController { } diff --git a/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/controller/SimpleControllerSupport.java b/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/controller/SimpleControllerSupport.java index 5c9fde4..2763700 100644 --- a/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/controller/SimpleControllerSupport.java +++ b/spring-boot-service-template-jpa/src/main/java/com/lanyuanxiaoyao/service/template/jpa/controller/SimpleControllerSupport.java @@ -1,12 +1,11 @@ package com.lanyuanxiaoyao.service.template.jpa.controller; import com.lanyuanxiaoyao.service.template.common.controller.GlobalResponse; +import com.lanyuanxiaoyao.service.template.common.controller.Query; import com.lanyuanxiaoyao.service.template.common.controller.SimpleController; import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper; import com.lanyuanxiaoyao.service.template.jpa.entity.SimpleEntity; import com.lanyuanxiaoyao.service.template.jpa.service.SimpleServiceSupport; -import java.util.List; -import java.util.Map; import java.util.function.Function; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.annotation.Transactional; @@ -131,10 +130,10 @@ public abstract class SimpleControllerSupport> list() throws Exception { + public GlobalResponse> list() throws Exception { var mapper = listItemMapper(); var result = service.list(); - return GlobalResponse.responseCrudData( + return GlobalResponse.responseListData( result .stream() .map(entity -> { @@ -159,13 +158,13 @@ public abstract class SimpleControllerSupport> list(@RequestBody com.lanyuanxiaoyao.service.template.common.controller.Query query) throws Exception { + public GlobalResponse> list(@RequestBody Query query) throws Exception { if (ObjectHelper.isNull(query)) { - return GlobalResponse.responseCrudData(List.of(), 0); + return GlobalResponse.responseListData(); } var mapper = listItemMapper(); var result = service.list(query); - return GlobalResponse.responseCrudData( + return GlobalResponse.responseListData( result.items() .map(entity -> { try {