1,在项目resources中添加配置文件
2,在模块中定一个CommonResultCode(类名自定义,可以参考SmsCommonResultCode)
自定义的key和三种语言配置文件中key要相对应
3.在项目中错题的地方直接抛出异常(ServiceException或者 GlobalException都可以,其他的也可以,需要自己实现)
不带参数的
带动态参数的
4,异常处理代码
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 业务异常
*/
@ExceptionHandler(ServiceException.class)
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
Integer code = e.getCode();
Object data = e.getData();
String message = errorMessageService.getMessage(e, null, request);
return StringUtils.isNotNull(code) ? (data != null ? AjaxResult.error(code, message, data) : AjaxResult.error(code, message)) : (data != null ?
AjaxResult.error(message, data) : AjaxResult.error(message));
}
}
@Service
public class ErrorMessageService implements ApplicationContextAware {
private static MessageSource messageSource;
public String getMessage(IErrorCode errorCode, String lang) {
try {
Locale locale = LangTypeEnum.getLangByCode(lang).getLocale();
return messageSource.getMessage(errorCode.getMsgKey(), null, locale);
} catch (NoSuchMessageException e) {
return errorCode.getMsgKey();
}
}
public String getMessage(IErrorCode errorCode, Object[] args, String lang) {
try {
Locale locale = LangTypeEnum.getLangByCode(lang).getLocale();
return messageSource.getMessage(errorCode.getMsgKey(), args, // 支持参数替换
locale);
} catch (NoSuchMessageException e) {
return messageSource.getMessage(errorCode.getMsgKey(), args, Locale.SIMPLIFIED_CHINESE);
}
}
public String getMessage(Throwable throwable, String lang, HttpServletRequest request) {
if (StringUtils.isBlank(lang) && request != null) {
lang = LangUtils.getLang(request).getCode();
}
if (lang == null) {
lang = LangTypeEnum.ZH_CN.getCode();
}
if (throwable instanceof ServiceException) {
ServiceException be = (ServiceException) throwable;
if (be.getErrorCode() == null) {
return be.getMessage();
}
return getMessage(be.getErrorCode(), be.getArgs(), lang);
}
return getMessage(be.getErrorCode(), be.getArgs(), lang);
}else if (throwable instanceof LimitException) {
LimitException be = (LimitException) throwable;
if (be.getErrorCode() == null) {
return be.getMessage();
}
return getMessage(be.getErrorCode(), be.getArgs(), lang);
}
return getMessage(CommonErrorCode.SYSTEM_ERROR, new Object[]{throwable.getMessage()}, lang);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
messageSource = applicationContext.getBean(MessageSource.class);
}
public static String getMessage(String propertiesKey) {
return getMessage(propertiesKey, propertiesKey);
}
public static String getMessage(String propertiesKey, String defaultMessage) {
return getMessage(propertiesKey, null, defaultMessage);
}
public static String getMessage(String propertiesKey, Object[] args, String defaultMessage) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String lang = null;
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
lang = LangUtils.getLang(request).getCode();
}
if (lang == null) {
lang = LangTypeEnum.ZH_CN.getCode();
}
return messageSource.getMessage(propertiesKey, args, defaultMessage, LangTypeEnum.getLangByCode(lang).getLocale());
}
}
public interface IErrorCode {
Integer getCode();
String getModule(); // 用于区分不同模块
String getMsgKey(); // 国际化消息的key
}
@Configuration
@AutoConfigureBefore(MessageSourceAutoConfiguration.class)
public class MessageConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}