feat: 返回code修改为integer
This commit is contained in:
parent
a5a4d4c7bb
commit
c67c7192e3
@ -1,10 +1,14 @@
|
||||
package cn.axzo.foundation.result;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.google.common.base.Strings;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@ -18,7 +22,7 @@ public class ApiResult<T> {
|
||||
|
||||
protected Integer httpCode;
|
||||
|
||||
protected String code;
|
||||
protected transient String errCode;
|
||||
|
||||
protected String msg;
|
||||
|
||||
@ -31,11 +35,17 @@ public class ApiResult<T> {
|
||||
public static <T> ApiResult<T> success(T data) {
|
||||
return ApiResult.<T>builder()
|
||||
.httpCode(SUCCESS_HTTP_CODE)
|
||||
.code(SUCCESS_CODE)
|
||||
.errCode(SUCCESS_CODE)
|
||||
.data(data)
|
||||
.build();
|
||||
}
|
||||
|
||||
//为了兼容前端需要的int类型, 这里返回一个单独的code
|
||||
public Integer getCode() {
|
||||
return Optional.ofNullable(Strings.emptyToNull(StringUtils.getDigits(errCode)))
|
||||
.map(Integer::parseInt).orElse(SUCCESS_HTTP_CODE);
|
||||
}
|
||||
|
||||
public static ApiResult error(IResultCode resultCode) {
|
||||
return error(resultCode.getHttpCode(), resultCode.getErrorCode(), resultCode.getErrorMessage());
|
||||
}
|
||||
@ -59,13 +69,13 @@ public class ApiResult<T> {
|
||||
public static ApiResult error(Integer httpCode, String errorCode, String errorMsg) {
|
||||
return ApiResult.builder()
|
||||
.httpCode(httpCode)
|
||||
.code(errorCode)
|
||||
.errCode(errorCode)
|
||||
.msg(errorMsg)
|
||||
.build();
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isSuccess() {
|
||||
return SUCCESS_CODE.equals(getCode());
|
||||
return SUCCESS_CODE.equals(getErrCode());
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@ public class ApiResultJsonConverter extends FastJsonHttpMessageConverter {
|
||||
|
||||
//只有抛出异常或者返回失败时才会设置header
|
||||
outputMessage.getHeaders().add(HTTP_CODE_HEADER_KEY, result.getHttpCode() + "");
|
||||
outputMessage.getHeaders().add(ERROR_CODE_HEADER_KEY, result.getCode());
|
||||
outputMessage.getHeaders().add(ERROR_CODE_HEADER_KEY, result.getErrCode());
|
||||
|
||||
//如果header中没有pretty. 按照定义好的fastJson的输出方式. 否则自定义美化输出
|
||||
writeJson(result, outputMessage);
|
||||
|
||||
@ -18,7 +18,7 @@ class ApiResultWrapper<T> extends ApiResult<T> {
|
||||
|
||||
@Builder(builderMethodName = "wrapperBuilder")
|
||||
public ApiResultWrapper(ApiResult result, AppRuntime appRuntime, ApiResultJsonConverter.ErrorMsgResolver msgResolver) {
|
||||
this.code = result.getCode();
|
||||
this.errCode = result.getErrCode();
|
||||
this.msg = result.getMsg();
|
||||
|
||||
this.httpCode = Optional.ofNullable(result.getHttpCode()).orElse(ResultCode.DEFAULT_HTTP_ERROR_CODE);
|
||||
@ -26,16 +26,16 @@ class ApiResultWrapper<T> extends ApiResult<T> {
|
||||
//如果是系统的或者一个完整的errorCode. 直接处理
|
||||
//目前已在系统中使用的ErrorCode格式都在6个字符以上;业务自定义code在6个字符以内(如果超过6个字符会直接返回);
|
||||
// 如:SYS_100001,SSO_100001,op-leads_5001001,10011002,1001X101;
|
||||
if (code.contains("_") || code.length() > 6) {
|
||||
if (errCode.contains("_") || errCode.length() > 6) {
|
||||
return;
|
||||
}
|
||||
|
||||
//没有appId时沿用之前的拼装逻辑"${appName}_${httpCode}${ErrorCode}"
|
||||
//存在appId时拼装逻辑调整为"${appId}${ErrorCode}"
|
||||
if (StringUtils.isEmpty(appRuntime.getAppId())) {
|
||||
this.code = Strings.nullToEmpty(appRuntime.getAppName()).toUpperCase() + "_" + getHttpCode() + result.getCode();
|
||||
this.errCode = Strings.nullToEmpty(appRuntime.getAppName()).toUpperCase() + "_" + getHttpCode() + result.getErrCode();
|
||||
} else {
|
||||
this.code = appRuntime.getAppId() + result.getCode();
|
||||
this.errCode = appRuntime.getAppId() + result.getErrCode();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public class RpcClientImpl implements RpcClient {
|
||||
Optional<String> resp = requestBySupplier(requestParams, () -> this.getHttpClient().execute(httpMethod, url, requestParams));
|
||||
ApiResult<T> result = converter.apply(resp.orElse(StringUtils.EMPTY));
|
||||
if (!result.isSuccess()) {
|
||||
throw new BusinessException(result.getCode(), result.getMsg());
|
||||
throw new BusinessException(result.getErrCode(), result.getMsg());
|
||||
}
|
||||
return result.getData();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user