欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对你服务的异常做详细的划分

程序员文章站 2022-04-23 23:40:20
...
package com.example.mongodemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.mongodemo.code.ResultCode;
import com.example.mongodemo.dto.ResultDTO;
import com.example.mongodemo.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.MethodNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;

import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.WebExchangeBindException;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author lyr
 * @create 2020/3/23 16:30
 * restful 架构
 */
@RestControllerAdvice
@ApiIgnore
@Order(-20)
@Slf4j
public class ExceptionController  extends BaseTemplateController{
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultDTO<ResultCode> exceptonShow(Exception ex) {
        log.error("服务器 基础异常 Exception {}",ex.getMessage());
        // ex.printStackTrace();
        return ResultDTO.<ResultCode>builder()
                .data(ResultCode.ERROR)
                .message("&服务器繁忙,可以反馈相关的问题给工作人员")
                .code(500)
                .build();

    }


    @ExceptionHandler(MethodNotSupportedException.class)
    @ResponseBody
    public ResultDTO methodNotSupport(MethodNotSupportedException ex) {
        log.error("服务器 请求方法不正确 {}",ex.getMessage());
        // ex.printStackTrace();
        return ResultDTO.<ResultCode>builder()
                .data(ResultCode.ERROR)
                .message("&接口不支持的请求方法")
                .code(500)
                .build();

    }
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<ResultDTO> serviceException(ServiceException ex) {
        log.error("服务器严重异常 {}",ex.getMessage());
        return ResponseEntity.ok(
                json("$服务运行异常 ",505)
        );
    }


    @ExceptionHandler(WebExchangeBindException.class)
    public  ResponseEntity<ResultDTO> missingValue(WebExchangeBindException ex) {
        log.error("服务器 参数绑定异常 {}",ex.getMessage());
        //Mono.just
        //返回 参数缺失的信息给前端
        return  ResponseEntity.ok(json("%参数绑定异常",505));

    }


    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultDTO methodMissParm(MissingServletRequestParameterException ex) {
        log.error("服务器 参数绑定异常 {}",ex.getMessage());
        //Mono.just
        //返回 参数缺失的信息给前端
        return (json("!请求参数缺失",404));

    }




    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultDTO<JSONObject> nullPointerExceptionHandler(NullPointerException ex) {
        log.error("nullPointerExceptionHandler, msg:{}, exception:{}", ex.toString(), ex.getMessage());
        JSONObject response = new JSONObject();
        response.put("message", "Internal Server Error");
        return json(response,"err",505);
    }


    /*----- REQUEST ERROR -----*/
    @ExceptionHandler({ HttpMessageNotReadableException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResultDTO<JSONObject> requestNotReadable(HttpMessageNotReadableException ex) {
        log.error("HttpMessageNotReadableExceptionHandler, msg:{}, exception:{}", ex.toString(), ex.getMessage());
        JSONObject response = new JSONObject();
        response.put("message", "Bad Request,not readable");
        return json(response,"err",500);
    }



    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResultDTO<ResultCode> httpNotSupportexceptonShow(HttpRequestMethodNotSupportedException ex) {
        log.error("httpMethod not Support Exception {}",ex.getMessage());
        // ex.printStackTrace();
        return ResultDTO.<ResultCode>builder()
                .data(ResultCode.ERROR)
                .message("@ http method not support")
                .code(500)
                .build();

    }



}

相关标签: CRUD