美文网首页程序员
为你Springboot项目自定义一个通用的异常(实用干货)

为你Springboot项目自定义一个通用的异常(实用干货)

作者: 爱码士人员 | 来源:发表于2021-01-05 16:56 被阅读0次

前言

我们的项目通常来讲都是一个比较大的项目,包含了各种各样的服务。如果每个服务都以不同的方式返回异常信息,这样排查的时候就会比较凌乱。如果我们定义一个标准的异常处理体系。并在所有的服务中使用。那样开发起来就可以快速定位。页面也会更加的简单和直观。

本文开发环境基于springboot2.4,IDE环境是IDEA。这里从一个最简单的异常案例。逐步过渡到完全自定义自己的异常。

案例:Springboot查询数据库数据,发现返回的是null,就抛出异常。

OK,基于这个思想,看一下实现的思路。

一、简单案例代码实现

1、新建一个Springboot应用

image.png

2、新建dao包,创建User类

这个比较简单,代码如下:

public class User {

private int id;

private String name;

public User() {

}

public User(int id, String name) {

this.id = id;

this.name = name;

}

//getter和setter方法

//toString方法

}

3、新建service包,创建UserService

@Service

public class UserService {

public User findOne(int id){

//本来应该向数据库查询User,但是数据库没有

return null;

}

}

由于演示的是异常的案例,因此这里没有真正实现数据库的增删改查操作。当调用findOne方法时,直接返回为null即可。

4、新建controller包,创建UserController类

@RestController

public class UserController {

@Autowired

private UserService service;

@GetMapping("/users/{id}")

public User retriveUser(@PathVariable int id) 

throws UserNotFoundException {

User user= service.findOne(id);

if(user==null)

throw new UserNotFoundException("id: "+ id);

return user;

}

}

这里自定义了一个异常UserNotFoundException,当数据库查询的时候一旦发现返回值为null,就直接抛出这个异常。

5、在controller包下,创建UserNotFoundException类

public class UserNotFoundException extends RuntimeException {

public UserNotFoundException(String message){

super(message);

System.out.println("异常信息是:"+message);

}

}

6、postman测试

image

这时候进行测试会发现服务器代码会报错。我们的资源没有找到总不能提示服务器内部错误吧。现在对抛出的异常进行一个处理。

7、异常处理

@ResponseStatus(HttpStatus.NOT_FOUND)

public class UserNotFoundException extends RuntimeException {

public UserNotFoundException(String message){

super(message);

System.out.println("异常信息是:"+message);

}

}

我们将添加一个注释@ResponseStatus来生成状态:404 Not Found。当然还有其他的状态。这个可以根据自己的需要去返回。我们使用了HttpStatus.NOT_FOUND用户访问的时候,一旦抛出了异常就会显示404错误。这个你换成其他的状态,还会显示其他的信息。

8、重新测试

image.png

Spring Boot和Spring MVC框架的结合提供了错误处理。其内部已经自动配置一些默认异常处理。所以在开发中为所有服务配置一致的异常消息是很重要的。

二、通用的异常处理

1、添加依赖

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>5.3.2</version>

</dependency>

2、创建异常返回实体类ExceptionResponse

这个类的作用是,当有异常时,我们想要展示的信息。

public class ExceptionResponse {

private Date timestamp;

private String message;

private String detail;

public ExceptionResponse() { }

public ExceptionResponse(Date timestamp, String message, String detail) {

this.timestamp = timestamp;

this.message = message;

this.detail = detail;

}

public Date getTimestamp() {

return timestamp;

}

public String getMessage() {

return message;

}

public String getDetail() {

return detail;

}

}

这里只需要实现getter方法,setter方法就不需要。

3、创建通用异常处理类

@ControllerAdvice

@RestController

public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

//此方法主要处理所有的异常信息

@ExceptionHandler(Exception.class)

public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {

//当出现异常时,我们输出的信息,这里被封装在了ExceptionResponse

ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));

return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);

}

//当页面资源没有找到时,抛出的异常

@ExceptionHandler(UserNotFoundException.class)

public final ResponseEntity<Object> handleUserNotFoundExceptions(UserNotFoundException ex, WebRequest request) {

ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));

return new ResponseEntity(exceptionResponse, HttpStatus.NOT_FOUND);

}

}

很简单。里面有很多API,可以自己根据需要去查即可。

4、postman测试

image.png

万事大吉。赶快为你的程序自定义一个通用的异常处理程序吧。

相关文章

网友评论

    本文标题:为你Springboot项目自定义一个通用的异常(实用干货)

    本文链接:https://www.haomeiwen.com/subject/xcjwoktx.html