[첫 편]springboot 이상 트 랜 잭 션 로그 처리

30531 단어 자바
프로젝트 소개:
Java framework for enterprise web applications
  • Vue
  • SpringBoot 2.1.6
  • Tk.Mybatis 2.1.5
  • Swagger 2.9.2
  • SpringSession 2.1.7
  • PageHelper 2.1.5

  • [첫 번 째 이상 한 통 일 된 처리]
    이상 을 통일 적 으로 처리 하 였 으 며,@Aspect 와@RestController Advice 를 사용 하여 통일 적 으로 처리 하 였 습 니 다.
    @RestControllerAdvice
    @Slf4j
    public class TalExceptionHandler {
    
        /**
         * json      
         * @param cve
         * @param httpServletRequest
         * @return
         */
        @ExceptionHandler(ConstraintViolationException.class)
        public Response methodArgNotValidException(ConstraintViolationException cve,
                                                   HttpServletRequest httpServletRequest) {
            log.error(cve.getMessage(), cve);
            Set<ConstraintViolation<?>> cves = cve.getConstraintViolations();
            StringBuffer errorMsg = new StringBuffer();
            cves.forEach(ex -> errorMsg.append(ex.getMessage()));
            Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), errorMsg.toString());
            return response;
        }
    
        /**
         * param      
         * @param ex
         * @param request
         * @return
         */
        @ExceptionHandler({MethodArgumentNotValidException.class})
        public Response methodDtoNotValidException(Exception ex, HttpServletRequest request) {
            log.error(ex.getMessage(), ex);
            MethodArgumentNotValidException c = (MethodArgumentNotValidException) ex;
            List<ObjectError> errors = c.getBindingResult().getAllErrors();
            StringBuffer errorMsg = new StringBuffer();
            errors.stream().forEach(x -> {
                errorMsg.append(x.getDefaultMessage()).append(";");
            });
            Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), errorMsg.toString());
            return response;
        }
    
        /**
         * 400 - Bad Request
         */
        @ExceptionHandler({MissingServletRequestParameterException.class})
        public Response handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
            log.error("      ", e);
            Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()),
                    "      " + e.getParameterName());
            return response;
        }
    
        /**
         *         
         *
         * @param e
         * @param req
         * @return
         */
        @ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class})
        public Response httpMessageNotReadableException(Exception e, WebRequest req) {
            log.error(e.getMessage(), e);
            Response response = new Response(ObjectUtils.toString(HttpStatus.BAD_REQUEST.value()), "      ");
            return response;
        }
        
          /**
         * Service      
         *
         * @param e
         * @return
         * @throws Exception
         */
        @ExceptionHandler(ParamException.class)
        public Response paramException(ParamException e) {
            return new Response(e.getCode(), e.getMessage());
        }
    
        /**
         *       。  500
         *
         * @param e
         * @return
         * @throws Exception
         */
        @ExceptionHandler({Exception.class})
        public Response defaultErrorHandler(Exception e) {
            log.error("    Exception", e);
            Response response = new Response(TkResponseCode.ERROR_NORMAL, "    ");
            return response;
        }
    }
    
    /**
     * 
     *        
     * 
     * @author
     */
    public class Response<T> implements Serializable
    {
        private static final long serialVersionUID = 6766428592214605847L;
        
        /**
         *      
         */
        @ApiModelProperty(value = "      ")
        private T responseEntity;
        
        /**
         *       
         */
        @ApiModelProperty(value = "       ")
        private PageInfo<T> pageInfo;
        
        /**
         *        
         */
        @ApiModelProperty(value = "      ")
        private MessageResult serverResult;
        
        public Response()
        {
            this.serverResult = getServerResult();
        }
        
        public Response(T t)
        {
            this.responseEntity = t;
            this.serverResult = getServerResult();
        }
        
        public Response(List<T> list)
        {
            if (list instanceof Page)
            {
                this.pageInfo = new PageInfo<T>(list,((Page<T>) list).getPages());
            }
            else
            {
                this.pageInfo = new PageInfo<T>();
                this.pageInfo.setList(list);
                this.pageInfo.setTotal(null == list ? 0 : list.size());
            }
            this.serverResult = getServerResult();
        }
        
        public Response(String resultCode,String resultMessage,String internalMessage)
        {
            this.serverResult = new MessageResult();
            serverResult.setResultCode(resultCode);
            serverResult.setResultMsg(resultMessage);
            serverResult.setInternalMsg(internalMessage);
        }
        
        public Response(String resultCode,String resultMessage)
        {
            this(resultCode,resultMessage,resultMessage);
        }
    
        public Response(String resultCode)
        {
            this(resultCode, TkResponseGetEContent.getEContent(resultCode), TkResponseGetEContent.getEContent(resultCode));
        }
        
        public MessageResult getServerResult()
        {
            /**
             *      
             */
            if (serverResult == null)
            {
                serverResult = new MessageResult();
                serverResult.setResultCode(IStateCode.HTTP_200);
                serverResult.setResultMsg("    ");
            }
            return serverResult;
        }
        
        public void setServerResult(MessageResult serverResult)
        {
            this.serverResult = serverResult;
        }
        
        public T getResponseEntity()
        {
            return responseEntity;
        }
        
        public void setResponseEntity(T responseEntity)
        {
            this.responseEntity = responseEntity;
        }
        
        public PageInfo<T> getPageInfo()
        {
            return pageInfo;
        }
        
        public void setPageInfo(PageInfo<T> pageInfo)
        {
            this.pageInfo = pageInfo;
        }
    }
    
    

    좋은 웹페이지 즐겨찾기