1.yml文件(在resources/errorcode下)
module: mgr
errorcodes:
user:
login:
"0001": "用户名不能为空"
"0002": "密码不能为空"
"0003": "用户无效或者密码错误"
2.自定义EXCEPTION
public class GCloudException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String[] params;
public GCloudException(String msg, String... params){
super(msg);
setParams(params);
}
public GCloudException(Exception ex){
super(ex);
}
public String[] getParams() {
return params;
}
public void setParams(String[] params) {
this.params = params;
}
}
3.错误码文件初始化
/*
* @Desccription 错误码初始化,读取所有jar包中的错误码文件,把错误码和错误信息对应放到内存map中
*/
@Configuration
public class ErrorCodeConfig {
@Bean
public ErrorCodes errorCodes(){
ErrorCodes errorCodes = new ErrorCodes();
errorCodes.init();
return errorCodes;
}
}
public class ErrorCodes extends HashMap<String, String> {
private static final long serialVersionUID = 1L;
public String get(String errorCode, String errorMsg, Object... params){
String msg = this.get(errorCode);
if(!StringUtils.isBlank(msg)){
errorMsg = msg;
}
if(params != null && StringUtils.isNotBlank(errorMsg)){
try{
errorMsg = String.format(errorMsg, params);
}catch (Exception ex){
log.error(String.format("format error message error, msg=%s", errorMsg), ex);
}
}
return errorMsg;
}
@SuppressWarnings("unchecked")
public void init() {
Yaml yaml = new Yaml();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = null;
try{
resources = resolver.getResources("classpath*:errorcode/seccenter-errorcode-*.yml");
}catch(Exception re){
log.error("get resources error", re);
}
if(resources != null && resources.length > 0){
for(Resource resource : resources){
InputStream io = null;
Map<String, Object> errorObj = null;
try{
io = resource.getInputStream();
errorObj = (Map<String, Object>) yaml.load(io);
}catch(Exception ex){
log.error("read error code error", ex);
}finally {
if(io != null){
try {
io.close();
} catch (IOException e) {
log.error("InputStream closed error", e);
}
}
}
handleErrorCode(errorObj);
}
}
}
@SuppressWarnings("unchecked")
private void handleErrorCode(Map<String, Object> errorObj){
if(errorObj == null){
return;
}
String module = ObjectUtils.toString(errorObj.get("module"));
Map<String, Object> subModules = (Map<String, Object>)errorObj.get("errorcodes");
for (Map.Entry<String, Object> subMod : subModules.entrySet()) {
String subModStr = subMod.getKey();
Map<String, Object> functions = (Map<String, Object>)subMod.getValue();
for(Map.Entry<String, Object> func : functions.entrySet()){
String funcStr = func.getKey();
Map<String, String> errors = (Map<String, String>)func.getValue();
for(Map.Entry<String, String> err : errors.entrySet()){
//默认补全四个
String codeNum = StringUtils.leftPad(err.getKey(), 4, "0");
String errorcode = String.format("%s_%s_%s_%s", module, subModStr, funcStr, codeNum);
this.put(errorcode, err.getValue());
}
}
}
}
}
4.把GcloudExcption转成对应的错误码和错误信息
/*
* @Desccription 错误信息类,吧GcloudExcption转成对应的错误码和错误信息。
*/
public class ErrorInfo {
private final String DEFAULT_CODE = "-1";
private final String DEFAULT_MESSAGE = "系统异常,请联系管理员";
private String errorCode;
private String errorMessage;
public ErrorInfo(){
this.initMessage(null, null);
}
public ErrorInfo(GCloudException ex){
String errMsg = ex.getMessage();
String[] params = ex.getParams();
this.initMessage(errMsg, params);
}
private void initMessage(String errMsg, String[] params){
if(errMsg != null && !"".equals(errMsg)){
//只取第一个::
int pos = errMsg.indexOf("::");
if(pos != -1){
this.setErrorCode(errMsg.substring(0, pos));
this.setErrorMessage(errMsg.substring(pos + 2));
}else{
this.setErrorCode(errMsg);
}
}
if(errorCode == null || "".equals(errorCode)){
this.setErrorCode(DEFAULT_CODE);
this.setErrorMessage(DEFAULT_MESSAGE);
}else{
transCode(params);
}
}
private void transCode(Object[] params){
ErrorCodes errorCodes = SpringUtil.getApplicationContext().getBean(ErrorCodes.class);
String msg = errorCodes.get(errorCode, errorMessage, params);
if(StringUtils.isNotBlank(msg)){
this.setErrorMessage(msg);
}
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}