SpringBoot项目异常的统一处理
采用Spring提供了一个通用的异常处理器ControllerAdvice,可以非常方便的帮助我们实现统一的异常处理。
1.定义异常处理器
ControllerExceptionAdvice
package com.ats.dt.advice;
import com.ats.dt.exceptions.HandleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* 要让这个类生效,必须让项目可以扫描到这个类所在的包,将其加入Spring容器。启动类的注解上添加扫描包信息
*/
@ControllerAdvice //会拦截所有加了@Controller的类,相当于给所有的controller写了一个try
@Slf4j
public class ControllerExceptionAdvice {
/**
* 统一异常处理方法,@ExceptionHandler(HandleException.class)声明这个方法处理RuntimeException这样的异常
* @param e 捕获到的异常
* @return 返回给页面的状态码和信息
*/
@ExceptionHandler(HandleException.class) //相当于catch
public ResponseEntity<String> handleProjectException(HandleException e) {
return ResponseEntity
.status(e.getStatus())
.contentType(MediaType.APPLICATION_JSON)
.body(e.getMessage());
}
}
代码解读:
- @ControllerAdvice:添加了@ControllerAdvice的类,默认情况下,会拦截所有加了@Controller的类
@ExceptionHandler(HandleException.class):作用在方法上,声明要处理的异常类型,这里指定的是HandleException。因此任意Controller抛出HandleException都会被当前方法捕获和处理
- 参数:捕获到的异常
- 返回值:给页面的提示信息。这里返回值中不仅给出了状态码400,还给出了异常的message
2.自定义异常
package com.ats.dt.exceptions;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Getter
public class HandleException extends RuntimeException {
/**
* 异常状态码信息
*/
private Integer status;
public HandleException(int status) {
this.status = status;
}
public HandleException(int status, String message) {
super(message);
this.status = status;
}
/**
* cause清楚的定位到是哪里的错(异常的起源)
* @param status 状态码
* @param message 消息内容
* @param cause 异常起源
*/
public HandleException(int status, String message, Throwable cause) {
super(message, cause);
this.status = status;
}
public HandleException(int status, Throwable cause) {
super(cause);
this.status = status;
}
}
3.使用自定义异常
3.1 实体类
@Data
public class Item {
private Integer id;
private String name;
private Long price;
}
3.2controller层
package com.ats.dt.controller;
import com.ats.dt.service.impl.ItemService;
import com.ats.dt.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "item")
@CrossOrigin
public class ItemController {
@Autowired
private ItemService itemService;
@PostMapping(value = "test")
@ResponseBody
public ResponseEntity<Item> saveItem(Item item) {
Item result = itemService.saveItem(item);
// 新增成功, 返回201
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}
}
3.3 service层业务实现
package com.ats.dt.service.impl;
import com.ats.dt.exceptions.HandleException;
import com.ats.dt.vo.Item;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class ItemService {
public Item saveItem(Item item){
// 判断价格是否为空
if(item.getPrice() == null){
throw new HandleException(401,"价格不能为空");
}
if(item.getName() == null){
throw new HandleException(400,"名称不能为空");
}
// 随机生成id,模拟数据库的新增
int id = new Random().nextInt(100);
item.setId(id);
// 返回新增之后的对象,回显id
return item;
}
}
4.测试结果
制造异常,将价格设置空
5.注意
确保springBoot启动时能使异常处理器能够加载到
6、日志的统一处理
日志统一处理
还没有评论,来说两句吧...