自定义固定全局异常并使用i18n国际化处理(并拓展根据业务需求追加自定义提示内容)

11 篇文章 0 订阅
7 篇文章 0 订阅

欢迎添加微信交流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());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值