您的位置:首页 > 编程语言 > Java开发

Springcloud_ad-ad-common统一异常

2019-05-02 14:30 288 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_39246466/article/details/89761309

统一异常基类

package com.imooc.ad.exception;

/**
 * Created by Qinyi.
 */
public class AdException extends Exception {

    public AdException(String message) {
        super(message);
    }
}
 

异常入口

package com.imooc.ad.advice;

import com.imooc.ad.exception.AdException;
import com.imooc.ad.vo.CommonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Qinyi.
 */
@RestControllerAdvice//统一异常响应
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = AdException.class)//标识异常种类,处理异常
    public CommonResponse<String> handlerAdException(HttpServletRequest req,
                                                     AdException ex) {
        CommonResponse<String> response = new CommonResponse<>(-1,
                "business error");
        response.setData(ex.getMessage());
        return response;
    }
}
 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐