欢迎添加微信交流Java问题:17625089935
首先定义好 自己需要返回信息的枚举ResponseCode,然后编写自定义异常类AppException,编写返回体JxResp,修后编写加强后的controller ExceptionController,最后将i18n放入resource文件夹中,并在配置文件中配置好路径
上代码:
package nbpt.ts.zhaf;
public enum ResponseCode {
NORMAL_ERROR(99999, ""),//通用异常码,所以没有固定固定异常信息
ERROR(10001, "zi.ding.yi.error"),
;
private int code;
private String key;
ResponseCode(int code, String key) {
this.code = code;
this.key = key;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
package nbpt.ts.zhaf;
public class AppException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int code;
private String key;//message key
public AppException(ResponseCode resCode) {
super(resCode.getKey());
this.code = resCode.getCode();
this.key = resCode.getKey();
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
package nbpt.ts.zhaf.entity;
import com.alibaba.fastjson.JSONObject;
/**
* APP 应答对象
*/
public class JxResp {
private int code;
private String msg="";
private int count;
private Object data = new JSONObject();
private Object tabHead;
public static JxResp OK(){
JxResp a = new JxResp();
a.setCode(0);
a.setMsg("");
return a;
}
public static JxResp OK(String msg){
JxResp a = new JxResp();
a.setCode(0);
a.setMsg(msg);
return a;
}
public static JxResp OK(String msg,Object obj){
JxResp a = new JxResp();
a.setCode(0);
a.setMsg(msg);
a.setData(obj);
return a;
}
public static JxResp Err(int code,String msg){
JxResp a = new JxResp();
a.setCode(code);
a.setMsg(msg);
return a;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Object getTabHead() {
return tabHead;
}
public void setTabHead(Object tabHead) {
this.tabHead = tabHead;
}
}
package nbpt.ts.zhaf;
import nbpt.ts.zhaf.entity.JxResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Locale;
/**
* @author zhangxuewei
*/
@ControllerAdvice
@ResponseBody
public class ExceptionController {
@Autowired
private MessageSource messageSource;
//自定义业务错误
@ExceptionHandler({AppException.class})
public JxResp appException(AppException ex) {
ex.printStackTrace();
Locale locale = LocaleContextHolder.getLocale();
return JxResp.Err(ex.getCode(), messageSource.getMessage(ex.getKey(), null, locale));
}
@ExceptionHandler({UndeclaredThrowableException.class})
public JxResp otherException(UndeclaredThrowableException ex) {
ex.printStackTrace();
return JxResp.Err(ResponseCode.NORMAL_ERROR.getCode(), ex.getUndeclaredThrowable().getMessage());
}
//其他错误
@ExceptionHandler({Exception.class})
public JxResp otherException(Exception ex) {
ex.printStackTrace();
return JxResp.Err(ResponseCode.NORMAL_ERROR.getCode(), ex.getMessage());
}
}
i18n文件获取:https://gitee.com/zxwAdd/i18n
配置路径:
spring.messages.basename=i18n/messages
使用:
if(true){
throw new AppException(ResponseCode.ERROR);
}
---
下面扩展自定义提示内容
package nbpt.ts.zhaf;
public class AppExceptionCustomMsg extends RuntimeException {
private static final long serialVersionUID = 1L;
private int code;
private String key;//message key
public AppExceptionCustomMsg(Integer code, String msg) {
super(msg);
this.code = code;
this.key = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
在ExceptionController中追加方法
@ExceptionHandler({AppExceptionCustomMsg.class})
public JxResp AppExceptionCustomMsg(AppExceptionCustomMsg ex) {
ex.printStackTrace();
return JxResp.Err(ex.getCode(), ex.getMessage());
}
使用
if(true){
throw new AppExceptionCustomMsg(1, "当前登陆账号缺少物资:" + finishStoreGoods.getGoodsName());
}
---
如果你想使用 自定义枚举中的 提示信息
在AppException中再添加一个属性+构造方法
private String additionalMsg;//追加信息
public AppException(ResponseCode resCode, String additionalMsg) {
super(resCode.getKey());
this.code = resCode.getCode();
this.key = resCode.getKey();
this.additionalMsg = additionalMsg;
}
在ExceptionController修改appException()
//自定义业务错误
@ExceptionHandler({AppException.class})
public JxResp appException(AppException ex) {
ex.printStackTrace();
Locale locale = LocaleContextHolder.getLocale();
return JxResp.Err(ex.getCode(), messageSource.getMessage(ex.getKey(), null, locale) + ex.getAdditionalMsg());
}
使用
throw new AppException(ResponseCode.ERROR, ":缺少" + finishStoreGoods.getGoodsName());