refactor(web): 重构全局响应处理方案

引入 Graceful Response(一个Spring Boot技术栈下的优雅响应处理组件,可以帮助开发者完成响应数据封装、异常处理、错误码填充等过程,提高开发效率,提高代码质量)
This commit is contained in:
2024-08-06 23:54:06 +08:00
parent 9ec2e6b981
commit 0b41f2d10c
20 changed files with 419 additions and 777 deletions

View File

@@ -64,6 +64,11 @@ public class PropertiesConstants {
*/
public static final String WEB_CORS = WEB + StringConstants.DOT + "cors";
/**
* 响应配置
*/
public static final String WEB_RESPONSE = WEB + StringConstants.DOT + "response";
/**
* 链路配置
*/
@@ -74,11 +79,6 @@ public class PropertiesConstants {
*/
public static final String WEB_XSS = WEB + StringConstants.DOT + "xss";
/**
* 国际化配置
*/
public static final String WEB_I18N = WEB + StringConstants.DOT + "i18n";
/**
* 日志配置
*/

View File

@@ -1,43 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.continew.starter.core.exception;
/**
* 统一错误码异常
*
* @author Jasmine
* @since 2.2.0
*/
public class GlobalException extends Exception {
private ResultInfoInterface resultInfo;
public GlobalException() {
}
public GlobalException(ResultInfoInterface resultInfo) {
this.resultInfo = resultInfo;
}
public ResultInfoInterface getResultInfo() {
return this.resultInfo;
}
public void setResultInfo(ResultInfoInterface resultInfo) {
this.resultInfo = resultInfo;
}
}

View File

@@ -1,66 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.continew.starter.core.exception;
/**
* 接口返回码 所有业务异常都要继承该接口
*
* @author Jasmine
* @since 2.2.0
*/
public enum GlobalResultInfoEnum implements ResultInfoInterface {
/**
* 操作成功
*/
SUCCESS(200, "操作成功"),
/**
* 操作失败
*/
FAILED(500, "操作失败");
private int code;
private String messageKey;
private String defaultMessage;
GlobalResultInfoEnum(int code, String defaultMessage) {
this.code = code;
this.defaultMessage = defaultMessage;
}
GlobalResultInfoEnum(int code, String messageKey, String defaultMessage) {
this.code = code;
this.messageKey = messageKey;
this.defaultMessage = defaultMessage;
}
@Override
public int getCode() {
return this.code;
}
@Override
public String getMessageKey() {
return this.messageKey;
}
@Override
public String getDefaultMessage() {
return this.defaultMessage;
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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.
*/
package top.continew.starter.core.exception;
/**
* 接口返回码与消息 所有业务异常都要继承该接口
*
* @author Jasmine
* @since 2.2.0
*/
public interface ResultInfoInterface {
/**
* 获取编码
*
* @return String
*/
int getCode();
/**
* 国际化消息key
*
* @return
*/
default String getMessageKey() {
return "";
}
/**
* 获取默认消息 若从国际化文件里没有获取到值,就取默认值
*
* @return String
*/
String getDefaultMessage();
}