还想看更多文章的朋友可以访问我的个人博客
spring boot 的默认处理
spring boot 未进入到Controller
中的错误请求处理:
spring boot 对于数据校验错误的请求处理:
请求: PUT "/user/1"
测试数据:{ "id": 1, "username" : "@jincoco", "password": "123456", "email": "jincoco@email.com", "birthday": 1542700692000 }
校验约束:username 字段必须是数字、字母或_的组合

由于测试错误为字段校验,需要上传数据,此处为PUT
请求,尽模拟 APP 发送请求。
spring boot 进入到Controller
中的错误请求处理:
spring boot 对于REST
服务所抛出的异常处:理:
请求: GET "/user/jin"
抛出异常:UserNotFoundException("User not found")
浏览器访问:

模拟 APP 访问:

如何自定义错误处理?
针对 HTTP 状态码定义错误页面
仅对浏览器起作用
需要自己创建错误页面,以 HTTP 状态码为文件名称即可:

此时,再访问不存在的服务,服务器再遇到404
错误时,便会默认定向到我们自己定义的页面并向浏览器返回:

模拟 APP 访问时,spring boot 仍然默认返回 json 数据:

针对服务器服务抛出的异常自定义处理:
例子中,Service
层:
@Override
public User query(String username) {
return userRepository.findByUsername(username).orElseThrow(() ->new UserNotFoundException(username));
}
也就是说,当查询用户不存在时,抛出UserNotFoundException
异常:
public class UserNotFoundException extends RuntimeException {
private final String identification;
public UserNotFoundException(String identification) {
super("User [" + identification + "] not found");
this.identification = identification;
}
public String getIdentification() {
return identification;
}
}
要对服务所抛出的异常进行处理,需要在定义下面一个异常处理器:
@ControllerAdvice
public class ControllerExceptionHandler {
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND) //404
@ExceptionHandler(UserNotFoundException.class)
public Map<String, Object> handleUserNotFoundException(UserNotFoundException ex) {
Map<String, Object> result = new HashMap<>();
result.put("id", ex.getIdentification());
result.put("message", ex.getMessage());
return result;
}
}
这里主要涉及三个关键注解:
-
@ExceptionHandler
,指明被注解方法是对哪一类异常进行处理(单独使用,仅可处理所在Controller
中的异常) -
@ControllerAdvice
,其实与Controller
这类注解类似,也是一个@Component
,其与ExceptionHandler
配合使用可以在任意位置进行异常处理。 -
@ResponseStatus
,指定返回 HTTP 状态码
在未进行异常处理时,我们通过 APP 访问GET: /user/jin
时,得到的 HTTP 状态码是500
,指服务器错误,且除了错误信息User [jin] not found
外无其他数据。而经过异常处理器后,请求的结果如下:

很明显,服务器的相应结果已经按我所设想的返回了。
网友评论