Merge branch 'feature/code_int' into 'master'

Feature/code int

See merge request infra/axzo-framework-commons!31
This commit is contained in:
田立勇 2023-02-08 06:30:45 +00:00
commit e0f8249958
18 changed files with 154 additions and 49 deletions

View File

@ -71,7 +71,7 @@ public abstract class AbstractExceptionApiResultHandler<T extends Throwable> imp
log(status, request, errorMsg, error); log(status, request, errorMsg, error);
// 6.返回body // 6.返回body
return err(code, message); return err(Integer.parseInt(code), message);
} }
protected T getRealCause(T ex) { protected T getRealCause(T ex) {

View File

@ -37,7 +37,7 @@ public class StandardExceptionResultHandler extends AbstractExceptionApiResultHa
final String message; final String message;
if (error instanceof ApiException || error instanceof ServiceException) { if (error instanceof ApiException || error instanceof ServiceException) {
if (error instanceof ApiException) { if (error instanceof ApiException) {
code = ((ApiException) error).getCode(); code = String.valueOf(((ApiException) error).getCode());
message = errorMsg; message = errorMsg;
} else { } else {
String serviceCode = ((ServiceException) error).getCode(); String serviceCode = ((ServiceException) error).getCode();

View File

@ -66,7 +66,7 @@ public class GlobalErrorController extends AbstractErrorController {
String msg = getMessage(baseCode, status, cause, request); String msg = getMessage(baseCode, status, cause, request);
// 4.响应 // 4.响应
val body = ApiResult.err(code, msg); val body = ApiResult.err(Integer.parseInt(code), msg);
return new ResponseEntity<>(body, status); return new ResponseEntity<>(body, status);
} }

View File

@ -85,7 +85,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
} }
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
@ -111,7 +111,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
} }
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
} }
@ -136,7 +136,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
} }
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
} }
@ -162,7 +162,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
} }
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
} }
@ -185,7 +185,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
message = "Required parameter '" + field + "' is not present"; message = "Required parameter '" + field + "' is not present";
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
} }
@ -230,7 +230,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
} }
// 3.响应 // 3.响应
return ApiResult.err(code, message); return ApiResult.err(Integer.parseInt(code), message);
}); });
return super.handleExceptionInternal(ex, result, headers, status, request); return super.handleExceptionInternal(ex, result, headers, status, request);
} }

View File

@ -0,0 +1,53 @@
{
"groups": [
{
"sourceType": "cn.axzo.framework.boot.env.configoverride.ConfigOverrideProperties",
"name": "spring.config.override",
"type": "cn.axzo.framework.boot.env.configoverride.ConfigOverrideProperties"
}
],
"properties": [
{
"sourceType": "cn.axzo.framework.boot.env.configoverride.ConfigOverrideProperties",
"name": "enabled",
"type": "java.lang.Boolean",
"defaultValue": false
},
{
"sourceType": "cn.axzo.framework.boot.env.configoverride.ConfigOverrideProperties",
"name": "props-file",
"type": "java.lang.String",
"defaultValue": "override"
},
{
"name": "system.properties.fetch-local-ip",
"type": "java.lang.Boolean",
"defaultValue": true
},
{
"name": "system.properties.fetch-local-ip-timeout-seconds",
"type": "java.lang.Integer",
"defaultValue": 2
},
{
"name": "spring.application.id",
"type": "java.lang.String",
"defaultValue": "000"
},
{
"name": "spring.profiles.default",
"type": "java.lang.String",
"defaultValue": "local"
},
{
"name": "spring.profiles.check-active",
"type": "java.lang.Boolean",
"defaultValue": true
},
{
"name": "logging.file-enabled",
"type": "java.lang.Boolean",
"defaultValue": true
}
]
}

View File

@ -0,0 +1,18 @@
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
cn.axzo.framework.boot.env.UnicodePropertiesPropertySourceLoader
# Application Listeners
org.springframework.context.ApplicationListener=\
cn.axzo.framework.boot.logging.log4j2.Log4j2MDCSetupListener,\
cn.axzo.framework.boot.logging.log4j2.Log4j2MDCListener,\
cn.axzo.framework.boot.system.SystemPropertiesListener,\
cn.axzo.framework.boot.DefaultProfileOverrideListener,\
cn.axzo.framework.boot.env.CheckActiveProfilesListener,\
cn.axzo.framework.boot.script.ScriptListener
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
cn.axzo.framework.boot.env.configoverride.ConfigOverrideEnvironmentPostProcessor,\
cn.axzo.framework.boot.env.devtools.DevToolsPropertyPostProcessor,\
cn.axzo.framework.boot.DynamicBannerEnvironmentPostProcessor

View File

@ -0,0 +1,17 @@
# 关闭log4j2在加载配置文件期间由于找不到配置文件而打印的错误日志
# spring-boot已经封装了加载配置文件的过程log4j2无需再关心加载期
# 为了避免两个框架都去加载配置文件,文件名最好不要用标准的命名,官方建议加上-spring
# 这样一来log4j2就找不到配置文件了默认会打一条ERROR日志所以要关闭此期间的StatusLogger
# log4j2官网描述
# Before a configuration is found,
# status logger level can be controlled with system propertyorg.apache.logging.log4j.simplelog.StatusLogger.level.
# After a configuration is found,
# status logger level can be controlled in the configuration file with the "status" attribute,
# for example: <Configuration status="trace">.
# SpringBoot官网描述
# When possible we recommend that you use the -spring variants for your logging configuration.
# For example log4j2-spring.xml rather than log4j2.xml.
# If you use standard configuration locations, Spring cannot completely control http initialization.
org.apache.logging.log4j.simplelog.StatusLogger.level=off

View File

@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#log4j的shutdownHook会托管给spring
log4j.shutdownHookEnabled=false

View File

@ -46,12 +46,12 @@ public class ApiResultErrorDecoder implements ErrorDecoder {
} }
ApiResult<?> apiResult = JSON.parseObject(response.body().asInputStream(), ApiResult.class); ApiResult<?> apiResult = JSON.parseObject(response.body().asInputStream(), ApiResult.class);
String code = apiResult.getCode(); Integer code = apiResult.getCode();
if (code == null) { if (code == null) {
return new ServiceException("ApiResult code is null"); return new ServiceException("ApiResult code is null");
} }
IRespCode respCode = new RespCode(code, apiResult.getMsg()); IRespCode respCode = new RespCode(String.valueOf(code), apiResult.getMsg());
if (isHystrixPresent && status.is4xxClientError()) { if (isHystrixPresent && status.is4xxClientError()) {
return new ClientException(respCode); // 不熔断 return new ClientException(respCode); // 不熔断
} }

View File

@ -15,7 +15,7 @@ import static java.lang.String.format;
public class ApiException extends RuntimeException { public class ApiException extends RuntimeException {
@Getter @Getter
private final String code; private final Integer code;
@Getter @Getter
private final boolean badRequest; private final boolean badRequest;
@ -38,7 +38,7 @@ public class ApiException extends RuntimeException {
ApiException(boolean badRequest, String message, IRespCode code, Object... args) { ApiException(boolean badRequest, String message, IRespCode code, Object... args) {
super(format(message == null ? code.getMessage() : message, args)); super(format(message == null ? code.getMessage() : message, args));
this.code = code.getRespCode(); this.code = Integer.parseInt(code.getRespCode());
this.badRequest = badRequest; this.badRequest = badRequest;
} }
} }

View File

@ -16,7 +16,7 @@ import java.util.stream.Stream;
@Description("基础错误码") @Description("基础错误码")
public enum BaseCode implements IModuleRespCode{ public enum BaseCode implements IModuleRespCode{
SUCCESS("000", "success", 200) { SUCCESS("200", "success", 200) {
@Override @Override
public String getRespCode() { public String getRespCode() {
return "200"; return "200";

View File

@ -29,7 +29,7 @@ import static jodd.util.StringUtil.isNotBlank;
public abstract class ApiCoreResult<E> implements Result { public abstract class ApiCoreResult<E> implements Result {
@ApiModelProperty(value = "业务码", required = true, position = 1) @ApiModelProperty(value = "业务码", required = true, position = 1)
protected String code; protected Integer code;
@ApiModelProperty(value = "业务码说明", required = true, example = "成功", position = 2) @ApiModelProperty(value = "业务码说明", required = true, example = "成功", position = 2)
protected String msg; protected String msg;
@ -45,7 +45,7 @@ public abstract class ApiCoreResult<E> implements Result {
@Transient @Transient
public boolean isSuccess() { public boolean isSuccess() {
// 兼容 // 兼容
return code.equals("00000000") || of(getOKCodes()).anyMatch(respOKCode -> Objects.equals(this.code, respOKCode.getRespCode())); return code == 200 || of(getOKCodes()).anyMatch(respOKCode -> Objects.equals(String.valueOf(this.code), respOKCode.getRespCode()));
} }
@Transient @Transient
@ -58,7 +58,7 @@ public abstract class ApiCoreResult<E> implements Result {
return new IRespCode() { return new IRespCode() {
@Override @Override
public String getCode() { public String getCode() {
return code; return String.valueOf(code);
} }
@Override @Override
@ -83,9 +83,9 @@ public abstract class ApiCoreResult<E> implements Result {
private static final String GLOBAL_SUCCESS_CODE = System.getProperty("apiresult.success_code"); private static final String GLOBAL_SUCCESS_CODE = System.getProperty("apiresult.success_code");
public String getCode() { public Integer getCode() {
if (isNotBlank(GLOBAL_SUCCESS_CODE) && isSuccess()) { if (isNotBlank(GLOBAL_SUCCESS_CODE) && isSuccess()) {
return GLOBAL_SUCCESS_CODE; return Integer.parseInt(GLOBAL_SUCCESS_CODE);
} }
return this.code; return this.code;
} }

View File

@ -22,7 +22,7 @@ public class ApiListResult<E> extends ApiCoreResult<List<E>> {
} }
public static <E> ApiListResult<E> ok(List<E> data) { public static <E> ApiListResult<E> ok(List<E> data) {
return build(BaseCode.SUCCESS.getRespCode(), BaseCode.SUCCESS.getMessage(), data); return build(Integer.parseInt(BaseCode.SUCCESS.getRespCode()), BaseCode.SUCCESS.getMessage(), data);
} }
public static <E> ApiListResult<E> err(IRespCode code) { public static <E> ApiListResult<E> err(IRespCode code) {
@ -30,18 +30,18 @@ public class ApiListResult<E> extends ApiCoreResult<List<E>> {
} }
public static <E> ApiListResult<E> err(IRespCode code, String message) { public static <E> ApiListResult<E> err(IRespCode code, String message) {
return err(code.getRespCode(), message); return err(Integer.parseInt(code.getRespCode()), message);
} }
public static <E> ApiListResult<E> err(String message) { public static <E> ApiListResult<E> err(String message) {
return err(BaseCode.SERVER_ERROR.getRespCode(), message); return err(Integer.parseInt(BaseCode.SERVER_ERROR.getRespCode()), message);
} }
public static <E> ApiListResult<E> err(IRespCode code, List<E> data) { public static <E> ApiListResult<E> err(IRespCode code, List<E> data) {
return build(code.getRespCode(), code.getMessage(), data); return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
} }
public static <E> ApiListResult<E> err(String code, String message) { public static <E> ApiListResult<E> err(Integer code, String message) {
if (code == null) { if (code == null) {
return err(message); return err(message);
} }
@ -53,15 +53,15 @@ public class ApiListResult<E> extends ApiCoreResult<List<E>> {
} }
public static <E> ApiListResult<E> build(IRespCode code, List<E> data) { public static <E> ApiListResult<E> build(IRespCode code, List<E> data) {
return build(code.getRespCode(), code.getMessage(), data); return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
} }
public static <E> ApiListResult<E> build(String code, String message, List<E> data) { public static <E> ApiListResult<E> build(Integer code, String message, List<E> data) {
return new ApiListResult<>(code, message, data); return new ApiListResult<>(code, message, data);
} }
@ConstructorProperties({"code", "msg", "data"}) @ConstructorProperties({"code", "msg", "data"})
public ApiListResult(String code, String message, List<E> data) { public ApiListResult(Integer code, String message, List<E> data) {
super(code, message, data); super(code, message, data);
} }
} }

View File

@ -70,17 +70,17 @@ public class ApiPageResult<E> extends ApiCoreResult<PageData<E>>{
} }
public static <E> ApiPageResult<E> ok(List<E> data, Long total) { public static <E> ApiPageResult<E> ok(List<E> data, Long total) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, return build(total, Integer.parseInt(SUCCESS.getRespCode()), SUCCESS.getMessage(), data,
null, null, null); null, null, null);
} }
public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize) { public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, pageNumber, pageSize, null); return build(total, Integer.parseInt(SUCCESS.getRespCode()), SUCCESS.getMessage(), data, pageNumber, pageSize, null);
} }
public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize, public static <E> ApiPageResult<E> ok(List<E> data, Long total, Integer pageNumber, Integer pageSize,
PageVerbose verbose) { PageVerbose verbose) {
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data, pageNumber, pageSize, verbose); return build(total, Integer.parseInt(SUCCESS.getRespCode()), SUCCESS.getMessage(), data, pageNumber, pageSize, verbose);
} }
public static <E> ApiPageResult<E> err(IRespCode code) { public static <E> ApiPageResult<E> err(IRespCode code) {
@ -88,10 +88,10 @@ public class ApiPageResult<E> extends ApiCoreResult<PageData<E>>{
} }
public static <E> ApiPageResult<E> err(IRespCode code, String message) { public static <E> ApiPageResult<E> err(IRespCode code, String message) {
return err(code.getRespCode(), message); return err(Integer.parseInt(code.getRespCode()), message);
} }
public static <E> ApiPageResult<E> err(String code, String message) { public static <E> ApiPageResult<E> err(Integer code, String message) {
return build(null, code, message, null, null, null, null); return build(null, code, message, null, null, null, null);
} }
@ -101,21 +101,21 @@ public class ApiPageResult<E> extends ApiCoreResult<PageData<E>>{
public static <E> ApiPageResult<E> build(Long total, IRespCode code, List<E> data, public static <E> ApiPageResult<E> build(Long total, IRespCode code, List<E> data,
Integer pageNum, Integer pageSize) { Integer pageNum, Integer pageSize) {
return build(total, code.getRespCode(), code.getMessage(), data, pageNum, pageSize, null); return build(total, Integer.parseInt(code.getRespCode()), code.getMessage(), data, pageNum, pageSize, null);
} }
public static <E> ApiPageResult<E> build(Long total, String code, String message, List<E> data, public static <E> ApiPageResult<E> build(Long total, Integer code, String message, List<E> data,
Integer pageNum, Integer pageSize) { Integer pageNum, Integer pageSize) {
return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, null); return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, null);
} }
public static <E> ApiPageResult<E> build(Long total, String code, String message, List<E> data, public static <E> ApiPageResult<E> build(Long total, Integer code, String message, List<E> data,
Integer pageNum, Integer pageSize, PageVerbose verbose) { Integer pageNum, Integer pageSize, PageVerbose verbose) {
return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, verbose); return new ApiPageResult<>(total, code, message, data, pageNum, pageSize, verbose);
} }
@ConstructorProperties({"code", "msg", "data"}) @ConstructorProperties({"code", "msg", "data"})
public ApiPageResult(Long total, String code, String message, List<E> list, Integer pageNum, Integer pageSize, public ApiPageResult(Long total, Integer code, String message, List<E> list, Integer pageNum, Integer pageSize,
PageVerbose verbose) { PageVerbose verbose) {
PageData<E> data = new PageData<E>(); PageData<E> data = new PageData<E>();
data.setList(list); data.setList(list);

View File

@ -33,18 +33,18 @@ public class ApiResult<E> extends ApiCoreResult<E> {
} }
public static <E> ApiResult<E> err(IRespCode code, String message) { public static <E> ApiResult<E> err(IRespCode code, String message) {
return err(code.getRespCode(), message); return err(Integer.parseInt(code.getRespCode()), message);
} }
public static <E> ApiResult<E> err(IRespCode code, E data) { public static <E> ApiResult<E> err(IRespCode code, E data) {
return build(code.getRespCode(), code.getMessage(), data); return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
} }
public static <E> ApiResult<E> err(String message) { public static <E> ApiResult<E> err(String message) {
return err(SERVER_ERROR.getRespCode(), message); return err(Integer.parseInt(SERVER_ERROR.getRespCode()), message);
} }
public static <E> ApiResult<E> err(String code, String message) { public static <E> ApiResult<E> err(Integer code, String message) {
if (code == null) { if (code == null) {
return err(message); return err(message);
} }
@ -56,10 +56,10 @@ public class ApiResult<E> extends ApiCoreResult<E> {
} }
public static <E> ApiResult<E> build(IRespCode code, E data) { public static <E> ApiResult<E> build(IRespCode code, E data) {
return build(code.getRespCode(), code.getMessage(), data); return build(Integer.parseInt(code.getRespCode()), code.getMessage(), data);
} }
public static <E> ApiResult<E> build(String code, String message, E data) { public static <E> ApiResult<E> build(Integer code, String message, E data) {
return new ApiResult<>(code, message, data); return new ApiResult<>(code, message, data);
} }
@ -74,7 +74,7 @@ public class ApiResult<E> extends ApiCoreResult<E> {
} }
@ConstructorProperties({"code", "msg", "data"}) @ConstructorProperties({"code", "msg", "data"})
public ApiResult(String code, String message, E data) { public ApiResult(Integer code, String message, E data) {
super(code, message, data); super(code, message, data);
} }

View File

@ -139,7 +139,7 @@ public class ApiListResponse<E> extends ApiCoreResponse<List<E>, ApiListResult<E
return wrapper(ApiListResult.err(code, data)); return wrapper(ApiListResult.err(code, data));
} }
public <E> ApiListResponse<E> err(String code, String message) { public <E> ApiListResponse<E> err(Integer code, String message) {
return wrapper(ApiListResult.err(code, message)); return wrapper(ApiListResult.err(code, message));
} }
@ -151,7 +151,7 @@ public class ApiListResponse<E> extends ApiCoreResponse<List<E>, ApiListResult<E
return wrapper(ApiListResult.build(code, data)); return wrapper(ApiListResult.build(code, data));
} }
public <E> ApiListResponse<E> build(String code, String message, List<E> data) { public <E> ApiListResponse<E> build(Integer code, String message, List<E> data) {
return wrapper(ApiListResult.build(code, message, data)); return wrapper(ApiListResult.build(code, message, data));
} }

View File

@ -167,7 +167,7 @@ public class ApiPageResponse<E> extends ApiCoreResponse<PageData<E>, ApiPageResu
return wrapper(ApiPageResult.err(code, message)); return wrapper(ApiPageResult.err(code, message));
} }
public <E> ApiPageResponse<E> err(String code, String message) { public <E> ApiPageResponse<E> err(Integer code, String message) {
return wrapper(ApiPageResult.err(code, message)); return wrapper(ApiPageResult.err(code, message));
} }
@ -179,7 +179,7 @@ public class ApiPageResponse<E> extends ApiCoreResponse<PageData<E>, ApiPageResu
return wrapper(ApiPageResult.build(total, code, data, pageNum, pageSize)); return wrapper(ApiPageResult.build(total, code, data, pageNum, pageSize));
} }
public <E> ApiPageResponse<E> build(Long total, String code, String message, List<E> data, public <E> ApiPageResponse<E> build(Long total, Integer code, String message, List<E> data,
Integer pageNum, Integer pageSize) { Integer pageNum, Integer pageSize) {
return wrapper(ApiPageResult.build(total, code, message, data, pageNum, pageSize)); return wrapper(ApiPageResult.build(total, code, message, data, pageNum, pageSize));
} }

View File

@ -161,7 +161,7 @@ public class ApiResponse<E> extends ApiCoreResponse<E, ApiResult<E>> {
return wrapper(ApiResult.err(message)); return wrapper(ApiResult.err(message));
} }
public <E> ApiResponse<E> err(String code, String message) { public <E> ApiResponse<E> err(Integer code, String message) {
return wrapper(ApiResult.err(code, message)); return wrapper(ApiResult.err(code, message));
} }
@ -177,7 +177,7 @@ public class ApiResponse<E> extends ApiCoreResponse<E, ApiResult<E>> {
return wrapper(ApiResult.build(code, data)); return wrapper(ApiResult.build(code, data));
} }
public <E> ApiResponse<E> build(String code, String message, E data) { public <E> ApiResponse<E> build(Integer code, String message, E data) {
return wrapper(ApiResult.build(code, message, data)); return wrapper(ApiResult.build(code, message, data));
} }