spring全局异常处理

工具类


import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Json响应对象类
 * @author YorkHwang
 * @since 2017-06-21
 */
public class JsonResponse<T>{

    //成功
    public static final int SC_SUCCESS = 200;
    //服务端异常
    public static final int SC_SERVER_ERROR = 500;
    //客户端异常
    public static final int SC_CLIENT_ERROR = 400;

    public static final String MSG_SUCCESS = "success";
    public static final String MSG_SERVER_ERROR = "服务端异常,请联系管理员";

    private int code = SC_SUCCESS;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data = null;

    public JsonResponse() {
    }

    public int getCode() {
        return code;
    }

    public JsonResponse<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public JsonResponse<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }


    public JsonResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    public boolean succeed(){
        return this.code == SC_SUCCESS;
    }


    public static <T> JsonResponse success(T data) {
        return success(data, MSG_SUCCESS);
    }

    public static <T> JsonResponse success(T data, String message) {
        return new JsonResponse().setCode(SC_SUCCESS).setData(data).setMsg(message);
    }

    public static <T> JsonResponse serverError(String message) {
        return new JsonResponse().setCode(SC_SERVER_ERROR).setMsg(message);
    }


    public static <T> JsonResponse serverError() {
        return new JsonResponse().setCode(SC_SERVER_ERROR).setMsg(MSG_SERVER_ERROR);
    }

    public static <T> JsonResponse parameterError(String message) {
        return new JsonResponse().setCode(SC_CLIENT_ERROR).setMsg(message);
    }

}

public class BusinessException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    protected final String    code;

    public BusinessException() {
        this.code = null;
    }

    public BusinessException(final String message) {
        super(message);
        this.code = null;
    }

    public BusinessException(final Throwable cause) {
        super(cause);
        this.code = null;
    }

    public BusinessException(final String message, final Throwable cause) {
        super(message, cause);
        this.code = null;
    }

    public BusinessException(final String code, final String message) {
        super(message);
        this.code = code;
    }

    public BusinessException(final String code, final String message, final Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public String getCode() {
        return this.code;
    }

}

全局异常处理类


import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class ExceptionConfig {

	@ExceptionHandler
	@ResponseBody
	public Object BusinessExceptionHandler(BusinessException e) {
		log.error("======================运行时异常信息======================", e);
		JsonResponse json = JsonResponse.serverError().setMsg(e.getMessage());
		return json;
	}
	@ExceptionHandler
	@ResponseBody
	public Object ExceptionHandler(Exception e) {
		log.error("======================Exception异常信息======================", e);
		JsonResponse json = JsonResponse.serverError().setMsg(e.getMessage());
		return json;
	}
	/*
	 * @ExceptionHandler
	 * 
	 * @ResponseBody public Object ExceptionHandler(Exception e) { if(e instanceof
	 * ArithmeticException) { log.error(
	 * "======================ArithmeticException异常信息======================",e);
	 * JsonResponse json = JsonResponse.serverError().setMsg("算数异常。"); return json;
	 * }else if(e instanceof NullPointerException) { log.error(
	 * "======================NullPointerException异常信息======================",e);
	 * JsonResponse json = JsonResponse.serverError().setMsg("空指针异常。"); return json;
	 * }else if(e instanceof BindException) { log.error(
	 * "======================NullPointerException异常信息======================",e);
	 * JsonResponse json = JsonResponse.serverError().setMsg("对象校验异常。"); return
	 * json; }else {
	 * log.error("======================Exception异常信息======================",e);
	 * JsonResponse json = JsonResponse.serverError().setMsg(e.getMessage()); return
	 * json; } }
	 */
}

controller测试类


import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctx.bean.Student;

@RestController
public class StudentController {
	@GetMapping("/fun")
	public Student fun(@Valid Student student) {
		int a=1/0;
		return student;
	}
}

实体类


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import lombok.Data;

@Data
public class Student {
	@NotBlank(message = "学号不能为空。")
	private String no;
	@NotBlank(message = "姓名不能为空。")
	private String name;
	@NotNull
	private Integer  age;
}

请求测试

#测试参数校验
http://localhost:8080/fun?no=1&name=zhangsan
#测试方法内部异常
http://localhost:8080/fun?no=1&name=zhangsan&age=2

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值