12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package com.jkcredit.invoice.config;
- import com.jkcredit.invoice.util.RespR;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.HttpStatus;
- import org.springframework.validation.BindException;
- import org.springframework.validation.FieldError;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import java.util.List;
- /**
- * @description:
- * @author: sunzhaoning
- * @create: 2019-05-29 13:43
- * @version: V1.0
- **/
- @Slf4j
- @RestControllerAdvice
- public class GlobalExceptionHandler {
- /**
- * 全局异常
- *
- * @param e
- * @return RespR
- */
- @ExceptionHandler(Exception.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- public RespR exception(Exception e) {
- log.error("全局异常信息,异常信息为:{}", e.getMessage(), e);
- return new RespR<>(e);
- }
- /**
- * validation Exception
- *
- * @param exception
- * @return RespR
- */
- @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
- @ResponseStatus(HttpStatus.OK)
- public RespR bodyValidExceptionHandler(MethodArgumentNotValidException exception) {
- List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
- RespR result = new RespR();
- result.setCode(1);
- result.setMsg(fieldErrors.get(0).getDefaultMessage());
- log.warn(fieldErrors.get(0).getDefaultMessage());
- return result;
- }
- }
|