Java, 原创, 服务器,

Spring boot 请求参数验证不通过,统一处理异常统一返回结果处理

Java 开发Web项目,请求参数验证不通过后,不希望抛出异常,而是能把错误信息返回给前端。AOP 实现方式 使用 注解 ControllerAdvice

我们使用 com.alibaba.cola.dto.Response

整体代码如下

package cn.test.fcs.aop;

import com.alibaba.cola.dto.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create By focusmedia<chenxue4076@163.com>
 * File Name ValidParamExceptionHandler.java
 * Created Date 2020/11/24
 * Created Time 9:15
 */
@Slf4j
@ControllerAdvice
public class ValidParamExceptionHandler {


    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuffer stringBuffer = new StringBuffer(bindingResult.getFieldErrors().size() * 16);
        stringBuffer.append("无效参数:");
        for( int i = 0; i < bindingResult.getFieldErrors().size(); ++i) {
            if (i > 0) {
                stringBuffer.append(",");
            }
            FieldError fieldError = bindingResult.getFieldErrors().get(i);
            stringBuffer.append(fieldError.getField());
            stringBuffer.append(":");
            stringBuffer.append(fieldError.getDefaultMessage());
        }
        Response response = new Response();
        response.setErrCode(String.valueOf(HttpStatus.BAD_REQUEST.value()));
        response.setErrMessage(stringBuffer.toString());
        response.setSuccess(false);
        log.error( "handleMethodArgumentNotValidException "+ stringBuffer.toString());
        return response;
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Object handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
        String exMessage = ex.getMessage();
        Response response = new Response();
        response.setErrCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
        response.setErrMessage(exMessage);
        response.setSuccess(false);
        log.error( "handleMethodArgumentNotValidException "+ exMessage);
        return response;
    }

    @ExceptionHandler(HttpMessageNotWritableException.class)
    @ResponseBody
    public Object handleHttpMessageNotWritableException(HttpMessageNotWritableException ex) {
        String exMessage = ex.getMessage();
        Response response = new Response();
        response.setErrCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
        response.setErrMessage(exMessage);
        response.setSuccess(false);
        log.error( "handleHttpMessageNotWritableException "+ exMessage);
        return response;
    }
}

// end ValidParamExceptionHandler.java
//end file

这样当字段验证不通过,或者字段类型有问题就可以正常通知到前端。

另外一篇统一返回值的 AOP 请查看 记录Spring boot 项目 AOP Aspect 实现环绕通知,含参数获取,结果处理(统一返回参数),异常抛出

(639)

Related Post