-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d9fdf
commit ce08f28
Showing
8 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
continew-starter-core/src/main/java/top/continew/starter/core/exception/GlobalException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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:https://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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...-starter-core/src/main/java/top/continew/starter/core/exception/GlobalResultInfoEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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:https://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; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...w-starter-core/src/main/java/top/continew/starter/core/exception/ResultInfoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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:https://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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...starter-web/src/main/java/top/continew/starter/web/autoconfigure/i18n/I18nProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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:https://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.web.autoconfigure.i18n; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import top.continew.starter.core.constant.PropertiesConstants; | ||
|
||
/** | ||
* 国际化 配置属性 | ||
* | ||
* @author Jasmine | ||
* @since 2.2.0 | ||
*/ | ||
@ConfigurationProperties(prefix = PropertiesConstants.I18N) | ||
public class I18nProperties { | ||
|
||
/** | ||
* 国际化开启 true-开启, false-关闭 | ||
*/ | ||
private Boolean enabled; | ||
|
||
public Boolean getEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(Boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
continew-starter-web/src/main/java/top/continew/starter/web/util/MessageSourceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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:https://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.web.util; | ||
|
||
import cn.hutool.extra.spring.SpringUtil; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
|
||
/** | ||
* @author Jasmine | ||
* @since 2.2.0 | ||
*/ | ||
public class MessageSourceUtils { | ||
|
||
private static final MessageSource messageSource = SpringUtil.getBean(MessageSource.class); | ||
|
||
private static final Object[] emptyArray = new Object[] {}; | ||
|
||
public static String getMessage(String key) { | ||
return getMessage(key, emptyArray); | ||
} | ||
|
||
public static String getMessage(String key, String defaultMessage) { | ||
return getMessage(key, defaultMessage, emptyArray); | ||
} | ||
|
||
public static String getMessage(String msgKey, Object... args) { | ||
return getMessage(msgKey, msgKey, args); | ||
} | ||
|
||
public static String getMessage(String msgKey, String defaultMessage, Object... args) { | ||
try { | ||
return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale()); | ||
} catch (Exception e) { | ||
return defaultMessage; | ||
} | ||
} | ||
|
||
} |