一般情況下確實對應不到國際化, 但是無奈老闆說, 我們要做國際化, 作爲boss drive develop 的一員, 還能有啥辦法, 我也很絕望啊.,那就做唄.
其實很簡單, 這裏就以我們拋出的異常進行封裝, 在英文和中文環境下兩套不同的錯誤.
1. 新建項目
2.在resources 文件夾下建立 i18n文件夾, 放錯誤信息.
新建文件 error_zh_CN.properties 放置中文錯誤信息
新建文件 error_en_US.properties 放置中文錯誤信息
3. 新建配置類:
package com.curefun.core.config;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.i18n.LocaleContextHolder;
/*
* 錯誤碼文本獲取
* 在項目resources目錄下新建i18n文件夾,錯誤碼文本按語言類型劃分文件,使用error開頭,後跟語言編碼,如error_zh_cn.properties,error_en_us.properties
*/
@PropertySource(value = { "classpath:i18n/error*.properties" })
public class ErrorCodeMessage {
private static ConcurrentHashMap<String, ResourceBundle> messages = new ConcurrentHashMap<String, ResourceBundle>();
/**
* 獲取錯誤碼文本
* @param key 錯誤碼編碼,如“10000”
* @param params String.format()佔位符對應文本
* @return
*/
public static String getMessage(String key, Object... params) {
// 獲取語言,這個語言是從header中的Accept-Language中獲取的,
// 會根據Accept-Language的值生成符合規則的locale,如zh、pt、en等
Locale locale = LocaleContextHolder.getLocale();
ResourceBundle message = messages.get(locale.getLanguage());
if (null == message) {
synchronized (messages) {
// 在這裏讀取配置信息
message = messages.get(locale.getLanguage());
if (null == message) {
message = ResourceBundle.getBundle("i18n/error", locale);
messages.put(locale.getLanguage() + "_" + locale.getCountry(), message);
}
}
}
// 此處獲取並返回message
if (null != params) {
return String.format(message.getString(key), params);
}
return message.getString(key);
}
/**
* 清空錯誤文本緩存map
*/
public static void flushMessage() {
messages.clear();
}
}
這裏進行配置的加載, 進行信息的緩存,
在業務代碼中只需要很簡單的拋出異常就好了
public class BusinessException extends RuntimeException {
private String code;
public BusinessException(String code) {
super(ErrorCodeMessage.getMessage(code));
this.code = code;
}
public BusinessException(String code, Object ...msgError) {
super(ErrorCodeMessage.getMessage(code,msgError));
this.code = code;
}
public String getCode() {
return this.code;
}
}
最後一步, 定義一個全局的異常處理器
import com.alibaba.fastjson.JSONObject;
import com.curefun.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author zk
* @Description: 全局異常處理
* @date 2019年10月29日15:08:35
*/
@Slf4j
@RestControllerAdvice
public class ExceptionHandleController {
/**
* 處理參數異常
*
* @param ex
*/
@ExceptionHandler(BusinessException.class)
public JSONObject handleParamException(BusinessException ex) {
JSONObject obj = new JSONObject();
obj.put("code", ex.getCode());
String message = ex.getMessage();
obj.put("msg", message);
obj.put("data", "");
log.error("發生異常:{}",ex);
return obj;
}
/**
* 處理參數缺失異常
*
* @param ex
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public JSONObject handleMissParamException(BusinessException ex) {
JSONObject obj = new JSONObject();
obj.put("code", 15000);
obj.put("msg", "缺少參數");
obj.put("data", "缺少參數");
log.error("發生異常:{}",ex);
return obj;
}
@ExceptionHandler(Exception.class)
public JSONObject handleException(Exception ex) {
JSONObject obj = new JSONObject();
obj.put("code", "15000");
obj.put("msg", "服務內部錯誤");
obj.put("data", "");
ex.printStackTrace();
log.error("發生異常:{}",ex.getCause());
return obj;
}
}
好像我又水了一篇文章, 哈哈哈 , 真容易, 果然花了5分鐘寫的文章 , 大概率是很水的文章. 湊合看下, 非常簡單的總結