| New file |
| | |
| | | package com.example.erp.config; |
| | | |
| | | import com.example.erp.common.Result; |
| | | import org.mybatis.logging.Logger; |
| | | import org.mybatis.logging.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpStatus; |
| | | 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 org.springframework.web.bind.annotation.ResponseStatus; |
| | | |
| | | @ControllerAdvice |
| | | public class ExceptionController { |
| | | private final Logger log = LoggerFactory.getLogger(ExceptionController.class); |
| | | |
| | | |
| | | @ResponseStatus(value = HttpStatus.BAD_REQUEST) |
| | | @ExceptionHandler(MethodArgumentNotValidException.class) |
| | | @ResponseBody |
| | | public Result getMessage(MethodArgumentNotValidException exception){ |
| | | Result result = new Result(); |
| | | result.setCode("400"); |
| | | String message = exception.getBindingResult().getFieldError().getDefaultMessage(); |
| | | // exception.getBindingResult().getFieldErrors(); 获取所有的错误信息 |
| | | result.setMsg("'"+exception.getBindingResult().getFieldError().getField()+"':"+message); |
| | | return result; |
| | | } |
| | | } |