1
0

feat: 优化Global返回值

This commit is contained in:
2025-08-21 10:36:21 +08:00
parent 6fbaa3de65
commit 6baefd1a8a
25 changed files with 1359 additions and 1440 deletions

View File

@@ -1,7 +1,5 @@
package com.lanyuanxiaoyao.service.template.controller; package com.lanyuanxiaoyao.service.template.controller;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalResponse;
/** /**
* 详情控制器接口,用于定义统一的获取实体详情的接口规范 * 详情控制器接口,用于定义统一的获取实体详情的接口规范
* *

View File

@@ -0,0 +1,77 @@
package com.lanyuanxiaoyao.service.template.controller;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class GlobalResponse<T> {
private static final int SUCCESS_STATUS = 0;
private static final int ERROR_STATUS = 500;
private static final String SUCCESS_MESSAGE = "OK";
private static final String ERROR_MESSAGE = "ERROR";
private Integer status;
private String message;
private T data;
private GlobalResponse() {
this(SUCCESS_STATUS, SUCCESS_MESSAGE, null);
}
private GlobalResponse(Integer status, String message) {
this(status, message, null);
}
private GlobalResponse(Integer status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public static GlobalResponse<Object> responseError() {
return responseError(ERROR_MESSAGE);
}
public static GlobalResponse<Object> responseError(String message) {
return new GlobalResponse<>(ERROR_STATUS, message);
}
public static GlobalResponse<Object> responseSuccess() {
return responseSuccess(SUCCESS_MESSAGE);
}
public static GlobalResponse<Object> responseSuccess(String message) {
return responseSuccess(message, null);
}
public static <E> GlobalResponse<E> responseSuccess(E data) {
return responseSuccess(SUCCESS_MESSAGE, data);
}
public static <E> GlobalResponse<E> responseSuccess(String message, E data) {
return new GlobalResponse<>(SUCCESS_STATUS, message, data);
}
public static GlobalResponse<Map<String, Object>> responseMapData(Map<String, Object> data) {
return responseSuccess(data);
}
public static GlobalResponse<Map<String, Object>> responseMapData(String key, Object value) {
return responseMapData(Map.of(key, value));
}
public static <T> GlobalResponse<Map<String, Object>> responseCrudData(Iterable<T> data, Integer total) {
return responseCrudData(data, total.longValue());
}
public static <T> GlobalResponse<Map<String, Object>> responseCrudData(Iterable<T> data, Long total) {
return responseMapData(Map.of("items", data, "total", total));
}
public static GlobalResponse<Map<String, Object>> responseDetailData(Object detail) {
return responseMapData("detail", detail);
}
}

View File

@@ -1,6 +1,6 @@
package com.lanyuanxiaoyao.service.template.controller; package com.lanyuanxiaoyao.service.template.controller;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalCrudResponse; import java.util.Map;
/** /**
* 列表控制器接口,用于定义统一的获取实体列表的接口规范 * 列表控制器接口,用于定义统一的获取实体列表的接口规范
@@ -65,7 +65,7 @@ public interface ListController<LIST_ITEM> {
* @return GlobalCrudResponse<LIST_ITEM> 返回实体列表 * @return GlobalCrudResponse<LIST_ITEM> 返回实体列表
* @throws Exception 查询过程中可能抛出的异常 * @throws Exception 查询过程中可能抛出的异常
*/ */
GlobalCrudResponse<LIST_ITEM> list() throws Exception; GlobalResponse<Map<String, Object>> list() throws Exception;
/** /**
* 根据查询条件获取实体列表 * 根据查询条件获取实体列表
@@ -74,5 +74,5 @@ public interface ListController<LIST_ITEM> {
* @return GlobalCrudResponse<LIST_ITEM> 返回符合条件的实体列表 * @return GlobalCrudResponse<LIST_ITEM> 返回符合条件的实体列表
* @throws Exception 查询过程中可能抛出的异常 * @throws Exception 查询过程中可能抛出的异常
*/ */
GlobalCrudResponse<LIST_ITEM> list(Query query) throws Exception; GlobalResponse<Map<String, Object>> list(Query query) throws Exception;
} }

View File

@@ -1,7 +1,5 @@
package com.lanyuanxiaoyao.service.template.controller; package com.lanyuanxiaoyao.service.template.controller;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalResponse;
/** /**
* 删除控制器接口,用于定义统一的删除实体对象的接口规范 * 删除控制器接口,用于定义统一的删除实体对象的接口规范
* *

View File

@@ -1,7 +1,5 @@
package com.lanyuanxiaoyao.service.template.controller; package com.lanyuanxiaoyao.service.template.controller;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalResponse;
/** /**
* 保存控制器接口,用于定义统一的保存实体对象的接口规范 * 保存控制器接口,用于定义统一的保存实体对象的接口规范
* *

View File

@@ -1,11 +1,10 @@
package com.lanyuanxiaoyao.service.template.controller; package com.lanyuanxiaoyao.service.template.controller;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalCrudResponse;
import com.lanyuanxiaoyao.service.template.controller.response.GlobalResponse;
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity; import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
import com.lanyuanxiaoyao.service.template.helper.ObjectHelper; import com.lanyuanxiaoyao.service.template.helper.ObjectHelper;
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport; import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -128,10 +127,10 @@ public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_
*/ */
@GetMapping(LIST) @GetMapping(LIST)
@Override @Override
public GlobalCrudResponse<LIST_ITEM> list() throws Exception { public GlobalResponse<Map<String, Object>> list() throws Exception {
var mapper = listItemMapper(); var mapper = listItemMapper();
var result = service.list(); var result = service.list();
return GlobalCrudResponse.responseCrudData( return GlobalResponse.responseCrudData(
result result
.stream() .stream()
.map(entity -> { .map(entity -> {
@@ -155,13 +154,13 @@ public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_
*/ */
@PostMapping(LIST) @PostMapping(LIST)
@Override @Override
public GlobalCrudResponse<LIST_ITEM> list(@RequestBody Query query) throws Exception { public GlobalResponse<Map<String, Object>> list(@RequestBody Query query) throws Exception {
if (ObjectHelper.isNull(query)) { if (ObjectHelper.isNull(query)) {
return GlobalCrudResponse.responseCrudData(List.of(), 0); return GlobalResponse.responseCrudData(List.of(), 0);
} }
var mapper = listItemMapper(); var mapper = listItemMapper();
var result = service.list(query); var result = service.list(query);
return GlobalCrudResponse.responseCrudData( return GlobalResponse.responseCrudData(
result.get() result.get()
.map(entity -> { .map(entity -> {
try { try {

View File

@@ -1,24 +0,0 @@
package com.lanyuanxiaoyao.service.template.controller.response;
public class GlobalCrudResponse<T> extends GlobalMapResponse {
public void setData(Iterable<T> list) {
setData("items", list);
}
public void setTotal(Long total) {
setData("total", total);
}
public void setTotal(Integer total) {
setTotal(total.longValue());
}
public void setData(Iterable<T> list, Long total) {
setData(list);
setTotal(total);
}
public void setData(Iterable<T> list, Integer total) {
setData(list, total.longValue());
}
}

View File

@@ -1,7 +0,0 @@
package com.lanyuanxiaoyao.service.template.controller.response;
public class GlobalDetailResponse extends GlobalMapResponse {
public void setDetail(Object detail) {
setData("detail", detail);
}
}

View File

@@ -1,15 +0,0 @@
package com.lanyuanxiaoyao.service.template.controller.response;
import java.util.HashMap;
import java.util.Map;
public class GlobalMapResponse extends GlobalResponse<Map<String, Object>> {
public GlobalMapResponse() {
setData(new HashMap<>());
}
public GlobalMapResponse setData(String key, Object value) {
getData().put(key, value);
return this;
}
}

View File

@@ -1,105 +0,0 @@
package com.lanyuanxiaoyao.service.template.controller.response;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class GlobalResponse<T> {
private static final int SUCCESS_STATUS = 0;
private static final int ERROR_STATUS = 500;
private static final String SUCCESS_MESSAGE = "OK";
private static final String ERROR_MESSAGE = "ERROR";
private Integer status;
private String message;
private T data;
public static GlobalResponse<Object> responseError() {
return responseError(ERROR_MESSAGE);
}
public static GlobalResponse<Object> responseError(String message) {
GlobalResponse<Object> response = new GlobalResponse<>();
response.setStatus(ERROR_STATUS);
response.setMessage(message);
return response;
}
public static GlobalResponse<Object> responseSuccess() {
GlobalResponse<Object> response = new GlobalResponse<>();
response.setStatus(SUCCESS_STATUS);
response.setMessage(SUCCESS_MESSAGE);
return response;
}
public static GlobalResponse<Object> responseSuccess(String message) {
GlobalResponse<Object> response = new GlobalResponse<>();
response.setStatus(SUCCESS_STATUS);
response.setMessage(message);
return response;
}
public static <E> GlobalResponse<E> responseSuccess(String message, E data) {
GlobalResponse<E> response = new GlobalResponse<>();
response.setStatus(SUCCESS_STATUS);
response.setMessage(message);
response.setData(data);
return response;
}
public static <E> GlobalResponse<E> responseSuccess(E data) {
GlobalResponse<E> response = new GlobalResponse<>();
response.setStatus(SUCCESS_STATUS);
response.setMessage(SUCCESS_MESSAGE);
response.setData(data);
return response;
}
public static GlobalMapResponse responseMapData() {
GlobalMapResponse response = new GlobalMapResponse();
response.setStatus(SUCCESS_STATUS);
response.setMessage(SUCCESS_MESSAGE);
return response;
}
public static GlobalMapResponse responseMapData(Map<String, Object> data) {
GlobalMapResponse response = responseMapData();
response.setData(data);
return response;
}
public static GlobalMapResponse responseMapData(String key, Object value) {
GlobalMapResponse response = responseMapData();
response.setData(key, value);
return response;
}
public static <T> GlobalCrudResponse<T> responseCrudData(Iterable<T> data) {
GlobalCrudResponse<T> response = new GlobalCrudResponse<>();
response.setStatus(SUCCESS_STATUS);
response.setMessage(SUCCESS_MESSAGE);
response.setData(data);
return response;
}
public static <T> GlobalCrudResponse<T> responseCrudData(Iterable<T> data, Integer total) {
GlobalCrudResponse<T> response = responseCrudData(data);
response.setTotal(total);
return response;
}
public static <T> GlobalCrudResponse<T> responseCrudData(Iterable<T> data, Long total) {
GlobalCrudResponse<T> response = responseCrudData(data);
response.setTotal(total);
return response;
}
public static GlobalDetailResponse responseDetailData(Object detail) {
GlobalDetailResponse response = new GlobalDetailResponse();
response.setDetail(detail);
return response;
}
}