SpringCloud微服務實戰——搭建企業級開發框架(七):自定義通用響應訊息及統一異常處理

全棧程式猿發表於2021-10-27

  平時開發過程中,無可避免我們需要處理各類異常,所以這裡我們在公共模組中自定義統一異常,Spring Boot 提供 @RestControllerAdvice 註解統一異常處理,我們在GitEgg_Platform中新建gitegg-platform-boot子工程,此工程主要用於Spring Boot相關功能的自定義及擴充套件。
1、修改gitegg-platform-boot的pom.xml,新增spring-boot-starter-web和swagger依賴,設定optional為true,讓這個包在專案之間依賴不傳遞。

        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-swagger</artifactid>
            <optional>true</optional>
        </dependency>

2、自定義通用響應訊息類,Result和PageResult,一個是普通響應訊息,一個是分頁響應訊息。
Result類:

package com.gitegg.platform.boot.common.base;

import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * @ClassName: Result
 * @Description: 自定義通用響應類
 * @author GitEgg
 * @date 2020年09月19日 下午9:24:50
 */
@ApiModel(description = "通用響應類")
@Getter
@ToString
public class Result<t> {

    @ApiModelProperty(value = "是否成功", required = true)
    private boolean success;
    
    @ApiModelProperty(value ="響應程式碼", required = true)
    private int code;

    @ApiModelProperty(value ="提示資訊", required = true)
    private String msg;

    @ApiModelProperty(value ="響應資料")
    private T data;
    
    /**
     * 
     * @param code
     * @param data
     * @param msg
     */
    private Result(int code, T data, String msg) {
        this.success = ResultCodeEnum.SUCCESS.code == code;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 
     * @param resultCodeEnum
     */
    private Result(ResultCodeEnum resultCodeEnum ) {
        this(resultCodeEnum.code, null, resultCodeEnum.msg);
    }

    /**
     * 
     * @param resultCodeEnum
     * @param msg
     */
    private Result(ResultCodeEnum resultCodeEnum , String msg) {
        this(resultCodeEnum, null, msg);
    }

    /**
     * 
     * @param resultCodeEnum
     * @param data
     */
    private Result(ResultCodeEnum resultCodeEnum , T data) {
        this(resultCodeEnum, data, resultCodeEnum.msg);
    }

    /**
     * 
     * @param resultCodeEnum
     * @param data
     * @param msg
     */
    private Result(ResultCodeEnum resultCodeEnum , T data, String msg) {
        this(resultCodeEnum.code, data, msg);
    }

    /**
     * 
     *
     * @param data 資料
     * @param <t>  T 響應資料
     * @
     */
    public static <t> Result<t> data(T data) {
        return data(data, ResultCodeEnum.SUCCESS.msg);
    }

    /**
     * 
     *
     * @param data 資料
     * @param msg  訊息
     * @param <t>  T 響應資料
     * @
     */
    public static <t> Result<t> data(T data, String msg) {
        return data(ResultCodeEnum.SUCCESS.code, data, msg);
    }

    /**
     * 
     *
     * @param code 狀態碼
     * @param data 資料
     * @param msg  訊息
     * @param <t>  T 響應資料
     * @
     */
    public static <t> Result<t> data(int code, T data, String msg) {
        return new Result<>(code, data, msg);
    }

    /**
     * 返回Result
     *
     * @param 
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> success() {
        return new Result<>(ResultCodeEnum.SUCCESS);
    }
    
    /**
     * 返回Result
     *
     * @param msg 訊息
     * @param <t> T 響應資料
     * @返回Result
     */
    public static <t> Result<t> success(String msg) {
        return new Result<>(ResultCodeEnum.SUCCESS, msg);
    }

    /**
     * 返回Result
     *
     * @param 
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> success(ResultCodeEnum resultCodeEnum ) {
        return new Result<>(resultCodeEnum);
    }

    /**
     * 返回Result
     *
     * @param 
     * @param msg   提示資訊
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> success(ResultCodeEnum resultCodeEnum , String msg) {
        return new Result<>(resultCodeEnum, msg);
    }
    
    /**
     * 返回Result
     *
     * @param <t> T 響應資料
     * @返回Result
     */
    public static <t> Result<t> error() {
        return new Result<>(ResultCodeEnum.ERROR, ResultCodeEnum.ERROR.msg);
    }

    /**
     * 返回Result
     *
     * @param msg 訊息
     * @param <t> T 響應資料
     * @返回Result
     */
    public static <t> Result<t> error(String msg) {
        return new Result<>(ResultCodeEnum.ERROR, msg);
    }


    /**
     * 返回Result
     *
     * @param code 狀態碼
     * @param msg  訊息
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> error(int code, String msg) {
        return new Result<>(code, null, msg);
    }

    /**
     * 返回Result
     *
     * @param 
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> error(ResultCodeEnum resultCodeEnum ) {
        return new Result<>(resultCodeEnum);
    }

    /**
     * 返回Result
     *
     * @param 
     * @param msg   提示資訊
     * @param <t>  T 響應資料
     * @返回Result
     */
    public static <t> Result<t> error(ResultCodeEnum resultCodeEnum , String msg) {
        return new Result<>(resultCodeEnum, msg);
    }
    
    /**
     * 
     * @param <t>
     * @param flag
     * @return
     */
    public static <t> Result<t> result(boolean flag) {
        return flag ? Result.success("操作成功") : Result.error("操作失敗");
    }

}

PageResult類:

package com.gitegg.platform.boot.common.base;

import java.util.List;

import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @ClassName: PageResult
 * @Description: 通用分頁返回
 * @author GitEgg
 * @date
 * @param <t>
 */
@Data
@ApiModel("通用分頁響應類")
public class PageResult<t> {

    @ApiModelProperty(value = "是否成功", required = true)
    private boolean success;

    @ApiModelProperty(value ="響應程式碼", required = true)
    private int code;

    @ApiModelProperty(value ="提示資訊", required = true)
    private String msg;

    @ApiModelProperty(value ="總數量", required = true)
    private long count;

    @ApiModelProperty(value ="分頁資料")
    private List<t> data;

    public PageResult(long total, List<t> rows) {
        this.count = total;
        this.data = rows;
        this.code = ResultCodeEnum.SUCCESS.code;
        this.msg = ResultCodeEnum.SUCCESS.msg;
    }
}

3、自定義通用響應訊息列舉類ResultCodeEnum。

package com.gitegg.platform.boot.common.enums;


/**
 * @ClassName: ResultCodeEnum
 * @Description: 自定義返回碼列舉
 * @author GitEgg
 * @date 2020年09月19日 下午11:49:45
 */
public enum ResultCodeEnum {

    /**
     * 成功
     */
    SUCCESS(200, "操作成功"),

    /**
     * 系統錯誤
     */
    ERROR(500, "系統錯誤"),

    /**
     * 操作失敗
     */
    FAILED(101, "操作失敗"),

    /**
     * 未登入/登入超時
     */
    UNAUTHORIZED(102, "登入超時"),

    /**
     * 引數錯誤
     */
    PARAM_ERROR(103, "引數錯誤"),

    /**
     * 引數錯誤-已存在
     */
    INVALID_PARAM_EXIST(104, "請求引數已存在"),

    /**
     * 引數錯誤
     */
    INVALID_PARAM_EMPTY(105, "請求引數為空"),

    /**
     * 引數錯誤
     */
    PARAM_TYPE_MISMATCH(106, "引數型別不匹配"),

    /**
     * 引數錯誤
     */
    PARAM_VALID_ERROR(107, "引數校驗失敗"),

    /**
     * 引數錯誤
     */
    ILLEGAL_REQUEST(108, "非法請求"),

    /**
     * 驗證碼錯誤
     */
    INVALID_VCODE(204, "驗證碼錯誤"),

    /**
     * 使用者名稱或密碼錯誤
     */
    INVALID_USERNAME_PASSWORD(205, "賬號或密碼錯誤"),

    /**
     *
     */
    INVALID_RE_PASSWORD(206, "兩次輸入密碼不一致"),

    /**
     * 使用者名稱或密碼錯誤
     */
    INVALID_OLD_PASSWORD(207, "舊密碼錯誤"),

    /**
     * 使用者名稱重複
     */
    USERNAME_ALREADY_IN(208, "使用者名稱已存在"),

    /**
     * 使用者不存在
     */
    INVALID_USERNAME(209, "使用者名稱不存在"),

    /**
     * 角色不存在
     */
    INVALID_ROLE(210, "角色不存在"),

    /**
     * 角色不存在
     */
    ROLE_USED(211, "角色使用中,不可刪除"),

    /**
     * 沒有許可權
     */
    NO_PERMISSION(403, "當前使用者無該介面許可權");

    public int code;

    public String msg;

    ResultCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

4、自定義異常類BusinessException和SystemException

package com.gitegg.platform.boot.common.exception;

import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * @ClassName: BusinessException
 * @Description: 業務處理異常
 * @author GitEgg
 * @date
 */
@Getter
@Setter
public class BusinessException extends RuntimeException {

    private int code;

    private String msg;

    public BusinessException() {
        this.code = ResultCodeEnum.FAILED.code;
        this.msg = ResultCodeEnum.FAILED.msg;
    }

    public BusinessException(String message) {
        this.code = ResultCodeEnum.FAILED.code;
        this.msg = message;
    }

    public BusinessException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

}
package com.gitegg.platform.boot.common.exception;

import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import lombok.Getter;

/**
 * @ClassName: SystemException
 * @Description: 系統處理異常
 * @author GitEgg
 * @date
 */
@Getter
public class SystemException extends RuntimeException {

    private int code;

    private String msg;

    public SystemException() {
        this.code = ResultCodeEnum.ERROR.code;
        this.msg = ResultCodeEnum.ERROR.msg;
    }

    public SystemException(String message) {
        this.code = ResultCodeEnum.ERROR.code;
        this.msg = message;
    }

    public SystemException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public SystemException(Throwable cause) {
        super(cause);
    }

    public SystemException(String message, Throwable cause) {
        super(message, cause);
    }

}

5、自定義統一異常處理類GitEggControllerAdvice.java

package com.gitegg.platform.boot.common.advice;

import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.platform.boot.common.enums.ResultCodeEnum;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.platform.boot.common.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.ui.Model;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GitEggControllerAdvice {

    /**
     * 服務名
     */
    @Value("${spring.application.name}")
    private String serverName;

    /**
     * 微服務系統標識
     */
    private String errorSystem;

    @PostConstruct
    public void init() {
        this.errorSystem = new StringBuffer()
                .append(this.serverName)
                .append(": ").toString();
    }

    /**
     * 應用到所有@RequestMapping註解方法,在其執行之前初始化資料繫結器
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {

    }

    /**
     * 把值繫結到Model中,使全域性@RequestMapping可以獲取到該值
     */
    @ModelAttribute
    public void addAttributes(Model model) {

    }

    /**
     * 全域性異常捕捉處理
     */
    @ExceptionHandler(value = {Exception.class})
    public Result handlerException(Exception exception, HttpServletRequest request) {
        log.error("請求路徑uri={},系統內部出現異常:{}", request.getRequestURI(), exception);
        Result result = Result.error(ResultCodeEnum.ERROR, errorSystem + exception.toString());
        return result;
    }

    /**
     * 非法請求異常
     */
    @ExceptionHandler(value = {
            HttpMediaTypeNotAcceptableException.class,
            HttpMediaTypeNotSupportedException.class,
            HttpRequestMethodNotSupportedException.class,
            MissingServletRequestParameterException.class,
            NoHandlerFoundException.class,
            MissingPathVariableException.class,
            HttpMessageNotReadableException.class
    })
    public Result handlerSpringAOPException(Exception exception) {
        Result result = Result.error(ResultCodeEnum.ILLEGAL_REQUEST, errorSystem + exception.getMessage());
        return result;
    }

    /**
     * 非法請求異常-引數型別不匹配
     */
    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    public Result handlerSpringAOPException(MethodArgumentTypeMismatchException exception) {
        Result result = Result.error(ResultCodeEnum.PARAM_TYPE_MISMATCH, errorSystem + exception.getMessage());
        return result;
    }

    /**
     * 非法請求-引數校驗
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public Result handlerMethodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException) {
        //獲取異常欄位及對應的異常資訊
        StringBuffer stringBuffer = new StringBuffer();
        methodArgumentNotValidException.getBindingResult().getFieldErrors().stream()
                .map(t -> t.getField()+"=>"+t.getDefaultMessage()+" ")
                .forEach(e -> stringBuffer.append(e));
        String errorMessage = stringBuffer.toString();
        Result result = Result.error(ResultCodeEnum.PARAM_VALID_ERROR, errorSystem + errorMessage);
        return result;
    }

    /**
     * 非法請求異常-引數校驗
     */
    @ExceptionHandler(value = {ConstraintViolationException.class})
    public Result handlerConstraintViolationException(ConstraintViolationException constraintViolationException) {
        String errorMessage = constraintViolationException.getLocalizedMessage();
        Result result = Result.error(ResultCodeEnum.PARAM_VALID_ERROR, errorSystem + errorMessage);
        return result;
    }

    /**
     * 自定義業務異常-BusinessException
     */
    @ExceptionHandler(value = {BusinessException.class})
    public Result handlerCustomException(BusinessException exception) {
        String errorMessage = exception.getMsg();
        Result result = Result.error(exception.getCode(), errorSystem + errorMessage);
        return result;
    }

    /**
     * 自定義系統異常-SystemException
     */
    @ExceptionHandler(value = {SystemException.class})
    public Result handlerCustomException(SystemException exception) {
        String errorMessage = exception.getMsg();
        Result result = Result.error(exception.getCode(), errorSystem + errorMessage);
        return result;
    }

}

6、重新將GitEgg-Platform進行install,在GitEgg-Cloud中的gitegg-service引入gitegg-platform-boot

<!--?xml version="1.0" encoding="UTF-8"?-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactid>GitEgg-Cloud</artifactid>
        <groupid>com.gitegg.cloud</groupid>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelversion>4.0.0</modelversion>

    <artifactid>gitegg-service</artifactid>
    <packaging>pom</packaging>
    <modules>
        <module>gitegg-service-base</module>
        <module>gitegg-service-bigdata</module>
        <module>gitegg-service-system</module>
    </modules>

    <dependencies>
        <!-- gitegg Spring Boot自定義及擴充套件 -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-boot</artifactid>
        </dependency>
        <!-- gitegg資料庫驅動及連線池 -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-db</artifactid>
        </dependency>
        <!-- gitegg mybatis-plus -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-mybatis</artifactid>
        </dependency>
        <!-- gitegg swagger2-knife4j -->
        <dependency>
            <groupid>com.gitegg.platform</groupid>
            <artifactid>gitegg-platform-swagger</artifactid>
        </dependency>
        <!-- spring boot web核心包 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <!-- spring boot 健康監控 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-actuator</artifactid>
        </dependency>
    </dependencies>

</project>

7、修改SystemController.java、ISystemService.java和SystemServiceImpl.java增加異常處理的測試程式碼

SystemController.java:

package com.gitegg.service.system.controller;

import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.service.system.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "system")
@AllArgsConstructor
@Api(tags = "gitegg-system")
public class SystemController {

    private final ISystemService systemService;

    @GetMapping(value = "list")
    @ApiOperation(value = "system list介面")
    public Object list() {
        return systemService.list();
    }


    @GetMapping(value = "page")
    @ApiOperation(value = "system page介面")
    public Object page() {
        return systemService.page();
    }

    @GetMapping(value = "exception")
    @ApiOperation(value = "自定義異常及返回測試介面")
    public Result<string> exception() {
        return Result.data(systemService.exception());
    }
}

ISystemService.java:

package com.gitegg.service.system.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitegg.service.system.entity.SystemTable;

import java.util.List;

public interface ISystemService {

    List<systemtable> list();

    Page<systemtable> page();

    String exception();
}

SystemServiceImpl.java:

package com.gitegg.service.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitegg.platform.boot.common.exception.BusinessException;
import com.gitegg.service.system.entity.SystemTable;
import com.gitegg.service.system.mapper.SystemTableMapper;
import com.gitegg.service.system.service.ISystemService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
@AllArgsConstructor
public class SystemServiceImpl implements ISystemService {

    private final SystemTableMapper systemTableMapper;

    @Override
    public List<systemtable> list() {
        return systemTableMapper.list();
    }

    @Override
    public Page<systemtable> page() {
        Page<systemtable> page = new Page<>(1, 10);
        List<systemtable> records = systemTableMapper.page(page);
        page.setRecords(records);
        return page;
    }

    @Override
    public String exception() {
        throw new BusinessException("自定義異常");
//        return "成功獲得資料";
    }
}

8、執行GitEggSystemApplication,開啟瀏覽器訪問:http://127.0.0.1:8001/doc.html,然後點選左側的異常處理介面,使用Swagger2進行測試,即可看到結果
image.png

本文原始碼在https://gitee.com/wmz1930/GitEgg 的chapter-07分支。

相關文章