返回值注解
This commit is contained in:
parent
0de4c0a672
commit
da02be6aba
@ -4,6 +4,8 @@ import java.beans.PropertyEditorSupport;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.czlis.common.core.domain.Result;
|
||||
import com.czlis.common.core.domain.ResultT;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -179,4 +181,22 @@ public class BaseController {
|
||||
public String getUsername() {
|
||||
return getLoginUser().getUsername();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 带泛型的转换(精细化转换)
|
||||
*/
|
||||
public static <T> ResultT<T> toResultT(Result oldResult, Class<T> clazz) {
|
||||
// 1. 空值校验:避免 oldResult 为 null 导致 NPE
|
||||
if (oldResult == null) {
|
||||
return ResultT.error("旧 Result 对象不能为空");
|
||||
}
|
||||
// 2. 调用 ResultT.fromResult(已实现类型校验+空值处理)
|
||||
try {
|
||||
return ResultT.fromResult(oldResult, clazz);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// 3. 类型不匹配时的兜底:返回错误结果,避免接口直接抛异常
|
||||
return ResultT.error("Result 转换失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@ import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.czlis.common.utils.JsonParamUtils;
|
||||
import com.czlis.common.utils.ServletUtils;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -21,13 +23,22 @@ import java.util.Map;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class Result {
|
||||
@ApiModelProperty(value = "响应码", example = "0", notes = "接口返回的状态码")
|
||||
private String code;
|
||||
@ApiModelProperty(value = "响应消息", example = "操作成功", notes = "接口返回的提示信息")
|
||||
private String msg;
|
||||
private Object data;
|
||||
private String url;// 下一步请求的URL
|
||||
private String method;// 下一步请求的方法(get/post等)
|
||||
private Object params;// 下一步请求的URL参数
|
||||
private Integer problemId;// 问题ID(之前提到的关键标识)
|
||||
@ApiModelProperty(value = "下一步请求URL", notes = "后续需要请求的接口地址")
|
||||
private String url;
|
||||
|
||||
@ApiModelProperty(value = "下一步请求方法", example = "GET", notes = "GET/POST/PUT/DELETE等")
|
||||
private String method;
|
||||
|
||||
@ApiModelProperty(value = "下一步请求参数", notes = "后续请求需要携带的参数")
|
||||
private Object params;
|
||||
|
||||
@ApiModelProperty(value = "问题ID", example = "1001", notes = "关键业务标识ID")
|
||||
private Integer problemId;
|
||||
public Result(String resultCode, String message) {
|
||||
super();
|
||||
this.code = resultCode;
|
||||
|
||||
@ -0,0 +1,89 @@
|
||||
package com.czlis.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 泛型返回类(继承原Result,无侵入改造)
|
||||
* 新代码使用此类,旧代码继续用Result
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true) // 继承父类的equals/hashCode
|
||||
@ApiModel(value = "ResultT<T>", description = "泛型统一返回结果封装类")
|
||||
public class ResultT<T> extends Result {
|
||||
@ApiModelProperty(value = "响应数据", notes = "接口返回的业务数据,类型由泛型T指定,可能为null")
|
||||
@SuppressWarnings("unchecked")
|
||||
public T getData() {
|
||||
// 父类的data是Object,强转为泛型T(添加注解抑制警告)
|
||||
return (T) super.getData();
|
||||
}
|
||||
|
||||
// ========== 构造方法(适配泛型) ==========
|
||||
public ResultT(String resultCode, String message) {
|
||||
super(resultCode, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 泛型数据构造方法(核心)
|
||||
*/
|
||||
public ResultT(String resultCode, String message, T data) {
|
||||
super(resultCode, message); // 调用父类构造
|
||||
super.setData(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 带problemId的构造方法(适配父类逻辑)
|
||||
*/
|
||||
public ResultT(String resultCode, String message, Integer problemId) {
|
||||
super(resultCode, message, problemId); // 调用父类带problemId的构造
|
||||
}
|
||||
|
||||
// ========== 静态工具方法(新代码使用) ==========
|
||||
public static <T> ResultT<T> success(T data) {
|
||||
return new ResultT<>("0", "操作成功", data);
|
||||
}
|
||||
|
||||
public static <T> ResultT<T> success(String message, T data) {
|
||||
return new ResultT<>("0", message, data);
|
||||
}
|
||||
|
||||
public static <T> ResultT<T> success() {
|
||||
return new ResultT<>("0", "操作成功");
|
||||
}
|
||||
public static <T> ResultT<T> msgbox(String message) {
|
||||
return new ResultT<>("1", message);
|
||||
}
|
||||
public static <T> ResultT<T> error() {
|
||||
return new ResultT<>("-1", "操作失败");
|
||||
}
|
||||
public static <T> ResultT<T> error(String message) {
|
||||
return new ResultT<>("-1", message);
|
||||
}
|
||||
|
||||
public static <T> ResultT<T> reback(String message, Integer problemId) {
|
||||
return new ResultT<>("5", message, problemId);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> ResultT<T> fromResult(Result oldResult, Class<T> clazz) {
|
||||
ResultT<T> resultT = new ResultT<>();
|
||||
resultT.setCode(oldResult.getCode());
|
||||
resultT.setMsg(oldResult.getMsg());
|
||||
resultT.setUrl(oldResult.getUrl());
|
||||
resultT.setMethod(oldResult.getMethod());
|
||||
resultT.setParams(oldResult.getParams());
|
||||
resultT.setProblemId(oldResult.getProblemId());
|
||||
|
||||
// 调用父类setData(Object),无冲突
|
||||
Object oldData = oldResult.getData();
|
||||
if (oldData != null && !clazz.isInstance(oldData)) {
|
||||
throw new IllegalArgumentException("数据类型不匹配!期望:" + clazz.getName() + ",实际:" + oldData.getClass().getName());
|
||||
}
|
||||
resultT.setData(oldData); // 复用父类setData,无冲突
|
||||
|
||||
return resultT;
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.czlis.common.core.domain.entity.lis;
|
||||
|
||||
import com.czlis.common.core.domain.BaseEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@ -12,6 +13,7 @@ import java.util.Date;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@ApiModel(value = "LabReqmain", description = "申请单表")
|
||||
@Data
|
||||
public class LabReqmain extends BaseEntity {
|
||||
@ApiModelProperty(value = "条码号")
|
||||
|
||||
@ -3,7 +3,9 @@ package com.czlis.liswork.controller.work;
|
||||
import com.czlis.common.annotation.Log;
|
||||
import com.czlis.common.core.controller.BaseController;
|
||||
import com.czlis.common.core.domain.Result;
|
||||
import com.czlis.common.core.domain.ResultT;
|
||||
import com.czlis.common.core.domain.entity.lis.LabPat;
|
||||
import com.czlis.common.core.domain.entity.lis.LabReqmain;
|
||||
import com.czlis.common.core.domain.entity.lis.LabResult;
|
||||
import com.czlis.common.core.page.TableDataInfo;
|
||||
import com.czlis.common.enums.BusinessType;
|
||||
@ -210,7 +212,7 @@ public class LisWorkController extends BaseController {
|
||||
}
|
||||
@ApiOperation("签收条码")
|
||||
@GetMapping("/querybarcode")
|
||||
public Result querybarcode(String sqh) {
|
||||
return lisWorkService.querybarcode(sqh);
|
||||
public ResultT<LabReqmain> querybarcode(String sqh) {
|
||||
return toResultT(lisWorkService.querybarcode(sqh),LabReqmain.class);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user