繁体   English   中英

Spring Boot - 处理用 BindException 包装的异常

[英]Spring Boot - handle exception wrapped with BindException

我正在寻找一种方法来处理在将请求参数绑定到 DTO 字段期间抛出的自定义异常。

我在 Spring Boot 应用程序中有一个 cantroller,如下所示

@GetMapping("/some/url")
public OutputDTO filterEntities(InputDTO inputDTO) {
    return service.getOutput(inputDTO);
}

输入 DTO 的字段很少,其中之一是枚举类型

public class InputDTO {

    private EnumClass enumField;
    private String otherField;

    /**
     * more fields
     */
}

用户将以这种方式点击 URL

localhost:8081/some/url?enumField=wrongValue&otherField=anyValue

现在,如果用户为 enumField 发送错误的值,我想用特定的消息抛出我的 CustomException。 枚举实例创建和抛出异常的过程在binder中实现

@InitBinder
public void initEnumClassBinder(final WebDataBinder webdataBinder) {
    webdataBinder.registerCustomEditor(
            EnumClass.class,
            new PropertyEditorSupport() {
                @Override
                public void setAsText(final String text) throws IllegalArgumentException {
                    try {
                        setValue(EnumClass.valueOf(text.toUpperCase()));
                    } catch (Exception exception) {
                        throw new CustomException("Exception while deserializing EnumClass from " + text, exception);
                    }
                }
            }
    );
}

问题是,当抛出异常时,无法处理它

@ExceptionHandler(CustomException.class)
public String handleException(CustomException exception) {
    // log exception
    return exception.getMessage();
}

Spring 用 BindException 包装初始异常。 该实例包含我的初始错误消息,但与其他对我来说多余的文本连接在一起。 我不认为解析和子字符串化该消息是好的......

我错过了什么吗? 从这里的初始 CustomException 获取消息的正确方法是什么?

在使用@ExceptionHandler注释方法进入控制器方法之前,您将无法处理抛出的异常。 Spring 在进入控制器之前处理这些异常,通过注册DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver处理程序。 这是 BindingException 的情况,当 Spring 无法绑定请求参数以匹配您的 InputDTO 对象时抛出。 您可以做的是注册您自己的处理程序(创建一个实现HandlerExceptionResolverOrdered接口的Component ),在处理错误时为其提供最高优先级,并根据需要处理异常。 您还必须注意BindException因为它包装了您的自定义异常CustomException.class

import java.io.IOException;

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger; import org.slf4j.LoggerFactory; 
import org.springframework.core.Ordered; 
import org.springframework.stereotype.Component; 
import org.springframework.validation.BindException; 
import org.springframework.validation.ObjectError; 
import org.springframework.web.servlet.HandlerExceptionResolver; 
import org.springframework.web.servlet.ModelAndView;


import yourpackage.CustomException;

@Component() 
public class BindingExceptionResolver implements HandlerExceptionResolver, Ordered {
    private static final Logger logger = LoggerFactory.getLogger(BindingExceptionResolver.class);

    public BindingExceptionResolver() {
    }

    private ModelAndView handleException(ObjectError objectError, HttpServletResponse response){
        if (objectError == null) return null;
        try {
            if(objectError.contains(CustomException.class)) {
                CustomException ex = objectError.unwrap(CustomException.class);
                logger.error(ex.getMessage(), ex);
                return handleCustomException(ex, response);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    protected ModelAndView handleCustomException(CustomException ex, HttpServletResponse response) throws IOException {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
        return new ModelAndView();
    }

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        try {
            if (ex instanceof org.springframework.validation.BindException) {
                BindException be = (BindException) ex;
                logger.debug("Binding exception in {} :: ({}) :: ({})=({})", be.getObjectName(), be.getBindingResult().getTarget().getClass(), be.getFieldError().getField(), be.getFieldError().getRejectedValue());
                return be.getAllErrors().stream()
                    .filter(o->o.contains(Exception.class))
                    .map(o ->handleException(o, response))
                    .filter(mv ->mv !=null)
                    .findFirst().orElse(null);
            }
        } catch (Exception handlerException) {
            logger.error("Could not handle exception", handlerException); 
        }
        return null;
    }


    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }

}

希望它有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM