一. 使用@ControllerAdvice+@ExceptionHandler注解
目的:
在控制层不需要再写如下的代码了
try{
// 业务逻辑
} catch(BusinessException b) {
// 日志打印
// 业务异常处理
} catch(Exception e) {
// 日志打印
// 非业务异常处理
}
- 构建一个springboot项目(引入依赖spring-boot-starter-web)
启动类如下:
package com.bigzone.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
// 暂不使用数据库, 所以取消数据源自动配置, 不然会报错
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
- 编写统一返回值类型JsonResult, 适用restful风格
package com.bigzone.springbootdemo.common;
import java.io.Serializable;
/**
* @author wangxq
* @date 2019/10/9
* 统一返回格式类
*/
public class JsonResult implements Serializable {
private static final long serialVersionUID = -4917497061692852616L;
private static final int SUCCESS_CODE = 0;
private static final int ERROR_CODE = 1;
private static final String SUCCESS_MSG = "成功";
private Integer code;
private String msg;
private Object data;
public JsonResult() {
}
public JsonResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public JsonResult(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static JsonResult success(Object data){
return new JsonResult(SUCCESS_CODE, SUCCESS_MSG, data);
}
public static JsonResult failure(String msg) {
return new JsonResult(ERROR_CODE, msg);
}
}
- 编写自定义异常类
package com.bigzone.springbootdemo.exception;
/**
* @author wangxq
* @date 2019/10/10
* 自定义业务异常类
*/
public class BusinessException extends RuntimeException {
public BusinessException() {
}
public BusinessException(String message) {
super(message);
}
}
- 编写全局异常处理类(@RestControllerAdvice = @ControllerAdvice + @ResponseBody + …)
package com.bigzone.springbootdemo.exception;
import com.bigzone.springbootdemo.common.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author wangxq
* @date 2019/10/10
* 全局异常处理类
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public JsonResult businessExceptionHandler(BusinessException b) {
System.out.println(b.getMessage());
return JsonResult.failure(b.getMessage());
}
@ExceptionHandler(Exception.class)
public JsonResult exceptionHandler(Exception e) {
e.printStackTrace();
return JsonResult.failure("请求失败!");
}
}
- 编写控制层
package com.bigzone.springbootdemo.controller;
import com.bigzone.springbootdemo.common.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wangxq
* @date 2019/10/9
*/
@RestController
@RequestMapping("/test/")
public class TestResultController {
@GetMapping("getData1")
public JsonResult getData1() {
int a = 10/0;
return JsonResult.success("success");
}
@GetMapping("getData2")
public JsonResult getData2() {
if (true) {
throw new BusinessException("业务异常...");
}
return JsonResult.success("success");
}
}
- 测试结果