webflux公共响应数据返回与全局异常处理总结

  • Post author:
  • Post category:其他



1.技术要点分析:Mono单次返回应用场景较多,本次封装以mono对象为主,往后会写一篇flux的使用场景与使用方式文章,大多数业务使用mono返回已经足够

2.以下为公共响应代码,以下代码使用了swagger2,webflux如何集成swagger3.0可参考我前一篇文章:

springboot3.3版,webflux集成swagger2最新3.0.0版,简单快捷高效

package com.lyr.business.demo.webflux.utils;

import com.lyr.common.core.enums.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Mono;

import java.io.Serializable;

/**
 * 返回结果
 *
 * @author lyr
 */
@Data
@ApiModel("公共响应")
public class RW<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    // 处理成功
    public static final String OK = "0000";
    // 成功描述
    public static final String OK_MSG = "操作成功";
    // 其他错误
    public static final String ERR = "9999";
    // 其他错误
    public static final String ERR_MSG = "操作失败";

    public static final RW SUCCESS = new RW(OK, OK_MSG);
    @ApiModelProperty("响应状态码")
    private String code = ERR;
    @ApiModelProperty("响应状态码描述")
    private String msg = "";
    @ApiModelProperty("响应数据data")
    private T data;

    public static <T> Mono<RW<T>> fail(Mono<T> monoData) {
        return createR(ERR, ERR_MSG,monoData);
    }

    public static <T> Mono<RW<T>> fail(String message,Mono<T> monoData) {
        return createR(ERR, message, monoData);
    }

    public static <T> Mono<RW<T>> fail(String code, String message,Mono<T> monoData) {
        return createR(code, message,monoData);
    }

    public static <T> Mono<RW<T>> success(String message,Mono<T> monoData) {
        return createR(OK, message,monoData);
    }

    public static <T> Mono<RW<T>> success(Mono<T> monoData) {
        return createR(OK, OK_MSG, monoData);
    }


    public static <T> Mono<RW<T>> withErrorCode(ErrorCode errorCode,Mono<T> monoData){
        return createR(errorCode.getCode(), errorCode.getMsg(),monoData);
    }

   private static <T> Mono<RW<T>> createR(String code, String msg, Mono<T> monoData){
      return monoData.map(data->{
           final RW rw = new RW();
           rw.setData(data);
           rw.setCode(code);
           rw.setMsg(msg);
           return rw;
       });

    }


    public RW() {
    }

    private RW(String code, String message) {
        this.code = code;
        this.msg = message;
    }

    private RW(String code, String message, T result) {
        this.code = code;
        this.msg = message;
        this.data = result;
    }

    @ApiModelProperty("本次请求是否成功")
    public boolean isSuccess(){
        return OK.equals(code);
    }

}


3.以下代码为全局异常处理代码,请求码可使用Enum,可自行根据业务进行整合封装,其中BusinessException为业务异常封装类,在这里进行了分开处理

/**
 * 全局异常处理类
 *
 * @author lyr
 */
@Slf4j
@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Mono<RW<Object>> handleCustomException(Exception e) {
        log.info("捕获到未处理的Exception异常:{}",e);
        return RW.fail("9999",e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Mono<RW<Object>> handleBusinessException(BusinessException e) {
        log.info("捕获到BusinessException异常:{}",e);
        return RW.fail(e.getCode(),e.getMessage());
    }
}


4.以上为公共响应和全局异常处理的核心代码,集成入项目运行测试即可



版权声明:本文为u013078871原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。