拦截不到@NotNull异常
问题描述:
拦截不到@NotNull注解产生的异常,自定义异常可以拦截到,麻烦老师帮忙看下什么原因
相关截图:
相关代码:
package com.imooc.imoocbbs.exception;
import com.imooc.imoocbbs.common.ApiResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/**
* 统一异常处理类
*/
@ControllerAdvice //用于拦截异常
public class GlobalExceptionHandler {
//系统异常处理
@ExceptionHandler(Exception.class) //规定处理异常类型
@ResponseBody
public Object handlerException(Exception e){
return ApiResponse.err(ImoocBBSExceptionEnum.SYSTEM_ERROR);
}
//自定义异常处理
@ExceptionHandler(ImoocBBSException.class)
@ResponseBody
public Object handlerImoocBBSException(ImoocBBSException e){
return ApiResponse.err(e.getCode(), e.getMsg());
}
//参数校验异常处理
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ApiResponse handleMethodArgumentNotVaildException(MethodArgumentNotValidException e){
return handleBindingResult(e.getBindingResult());
}
//绑定出问题时将异常处理成响应类型
private ApiResponse handleBindingResult(BindingResult result){
//把异常处理为对外暴露的提示
//创建异常list
//result中是否包含错误
List<String> list = new ArrayList<>();
if (result.hasErrors()){
//获取所有错误
List<ObjectError> allErrors = result.getAllErrors();
for (ObjectError objectError: allErrors){
// 获取错误信息
String message = objectError.getDefaultMessage();
list.add(message);
}
}
if (list.size() == 0)
return ApiResponse.err(ImoocBBSExceptionEnum.REQUEST_PARAM_ERROR);
return ApiResponse.err(ImoocBBSExceptionEnum.REQUEST_PARAM_ERROR.getCode(),list.toString());
}
}
package com.imooc.imoocbbs.common;
import com.imooc.imoocbbs.exception.ImoocBBSExceptionEnum;
/**
* API通用返回对象
* @param <T>
*/
public class ApiResponse <T>{
//状态码
private Integer status;
//信息
private String msg;
//数据
private T data;
//正常状态码和信息
private static final int OK_CODE = 10000;
private static final String OK_MSG = "SUCCESS";
public ApiResponse(Integer status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public ApiResponse(Integer status, String msg) {
this.status = status;
this.msg = msg;
}
public ApiResponse() {
this(OK_CODE, OK_MSG);
}
//调用成功
public static <T> ApiResponse success(){ return new ApiResponse();}
public static <T> ApiResponse success(T data){
ApiResponse<T> apiResponse = new ApiResponse<>();
apiResponse.setData(data);
return apiResponse;
}
//调用失败
public static <T> ApiResponse err(Integer code, String msg){
return new ApiResponse(code,msg);
}
public static <T> ApiResponse err(ImoocBBSExceptionEnum exceptionEnum){
return new ApiResponse(exceptionEnum.getCode(),exceptionEnum.getMsg());
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ApiResponse{" +
"status=" + status +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
package com.imooc.imoocbbs.controller;
import com.imooc.imoocbbs.common.ApiResponse;
import com.imooc.imoocbbs.model.pojo.User;
import com.imooc.imoocbbs.model.request.LoginRequest;
import com.imooc.imoocbbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
/**
* 用户模块控制器
*/
@RestController
public class UserController {
@Autowired
UserService userService;
/**
* 用户登陆
*/
@PostMapping("/login")
public ApiResponse login(HttpSession session, @Valid LoginRequest request){
User user = userService.login(request);
session.setAttribute("IMOOC_MALL_USER", user);
return ApiResponse.success(user);
}
}
package com.imooc.imoocbbs.model.request;
import javax.validation.constraints.NotNull;
public class LoginRequest {
@NotNull(message = "用户名不能为空")
private String username;
@NotNull(message = "密码不能为空")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
33
收起
正在回答 回答被采纳积分+1
2回答
啃大饼的瓜子脸0_0
2021-09-04 10:28:44
你可以看看这个文章,感觉解释的不错
https://blog.csdn.net/weixin_39787628/article/details/110509640
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星