From b91e787c6feb919881c07acbeffba7a587269f83 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 12 Aug 2022 19:11:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E6=95=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/base/rest/ServiceException.java | 26 ++++++++++++++++--- .../proxy/server/util/ParamCheckUtil.java | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java index a474466f..fd00c6b7 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.proxy.server.base.rest; import fun.asgc.neutrino.proxy.server.base.rest.constant.ExceptionConstant; import lombok.Getter; +import org.apache.commons.lang3.StringUtils; /** * @@ -50,8 +51,27 @@ public class ServiceException extends RuntimeException { } public static ServiceException create(ExceptionConstant constant, Object... params) { - int code = constant.getCode(); - String msg = String.format(constant.getMsg(), params); - return new ServiceException(code, msg); + return new ServiceException(constant.getCode(), format(constant.getMsg(), params)); + } + + /** + * 字符串格式化 + * @param template + * @param params + * @return + */ + public static String format(String template, Object[] params) { + if (StringUtils.isEmpty(template) || null == params || params.length == 0) { + return template; + } + String result = template; + for (Object param : params) { + int index = result.indexOf("{}"); + if (index == -1) { + return result; + } + result = result.substring(0, index) + param + result.substring(index + 2); + } + return result; } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java index 05b8e7b6..d561e4b4 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ParamCheckUtil.java @@ -40,4 +40,5 @@ public class ParamCheckUtil { throw ServiceException.create(ExceptionConstant.PARAMS_NOT_NULL, name); } } + }