@ExceptionHandler捕获不到MethodArgumentNotValidException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | package com.imooc.mall.exception; import com.imooc.mall.common.ApiRestResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * 描述: 处理统一异常的handler */ @ControllerAdvice public class GlobalExceptionHandler { private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler. class ); @ExceptionHandler (Exception. class ) @ResponseBody public Object handleException(Exception e) { log.error( "Default Exception: " , e); return ApiRestResponse.error(ImoocMallExceptionEnum.SYSTEM_ERROR); } @ExceptionHandler (ImoocMallException. class ) @ResponseBody public Object handleImoocMallException(ImoocMallException e) { log.error( "ImoocMallException: " , e); return ApiRestResponse.error(e.getCode(), e.getMessage()); } // @ExceptionHandler(MethodArgumentNotValidException.class) // @ResponseBody // public ApiRestResponse handleMethodArgumentNotValidException( MethodArgumentNotValidException e) { // log.error("MethodArgumentNotValidException" , e); // // return handleBindingResult(e.getBindingResult()); // } // // // public ApiRestResponse handleBindingResult(BindingResult result){ // List<String> errorMessage = new ArrayList(); // if (result.hasErrors()){ // List<ObjectError> allErrors = result.getAllErrors(); // for (ObjectError list: allErrors) { // String message = list.getDefaultMessage(); // errorMessage.add(message); // } // } // if (errorMessage.size() == 0){ // return ApiRestResponse.error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR); // } // // return ApiRestResponse.error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR.getCode() , errorMessage.toString()); // } @ExceptionHandler (MethodArgumentNotValidException. class ) @ResponseBody public ApiRestResponse handleMethodArgumentNotValidException( MethodArgumentNotValidException e) { log.error( "MethodArgumentNotValidException: " , e); return handleBindingResult(e.getBindingResult()); } private ApiRestResponse handleBindingResult(BindingResult 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 ApiRestResponse.error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR); } return ApiRestResponse .error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR.getCode(), list.toString()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.imooc.mall.controller; import com.imooc.mall.common.ApiRestResponse; import com.imooc.mall.common.Constant; import com.imooc.mall.exception.ImoocMallExceptionEnum; import com.imooc.mall.model.pojo.User; import com.imooc.mall.model.requert.AddCategoryReq; import com.imooc.mall.service.CategoryService; import com.imooc.mall.service.UserService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import javax.validation.Valid; @RestController public class CategoryController { @Resource UserService userService; @Resource CategoryService categoryService; @PostMapping ( "/admin/category/add" ) public ApiRestResponse addCategory(HttpSession session , @Valid AddCategoryReq addCategoryReq){ // if (addCategoryReq.getName() == null ||addCategoryReq.getType() == null // || addCategoryReq.getOrderNum() == null ||addCategoryReq.getParentId() == null){ // return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_Category); // } User user = (User)session.getAttribute(Constant.IMOOC_MALL_USER); if (user == null ){ return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN); } boolean f = userService.checkAdminRole(user); if (f){ categoryService.addCategory(addCategoryReq); return ApiRestResponse.success(); } else { return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.imooc.mall.model.requert; import javax.validation.constraints.Max; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class AddCategoryReq { @NotNull @Size (min = 2 , max = 5 ) private String name; @NotNull @Max ( 3 ) private Integer type; @NotNull private Integer parentId; @NotNull private Integer orderNum; public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getType() { return type; } public void setType(Integer type) { this .type = type; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this .parentId = parentId; } public Integer getOrderNum() { return orderNum; } public void setOrderNum(Integer orderNum) { this .orderNum = orderNum; } } |
相关代码按照课程上面的写了但是返回的却是
相关截图:
相关截图:
10
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧