如何处理枚举中空值的 InvalidFormatException

Posted

技术标签:

【中文标题】如何处理枚举中空值的 InvalidFormatException【英文标题】:How to handle InvalidFormatException for empty values on enumerations 【发布时间】:2021-09-02 00:09:57 【问题描述】:

。当一个空值被传递给底层的杰克逊解析器而不是一个低级错误时,我们想抛出一个验证错误。我们怎样才能避免这种情况?

Exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.company.model.Flower` from String "": not one of the values accepted for Enum class: [Lilly, Jasmine]
 at [Source: (PushbackInputStream); line: 1, column: 64] 
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:389)
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:186)
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158)
        at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:170)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)

【问题讨论】:

【参考方案1】:

假设您的 jackson-databind 版本 >= 2.12.0, 参考this article - Jackson 2.12 Most Wanted (4/5): CoercionConfig system


你可以在ObjectMapper中添加一个配置bean来添加枚举的强制设置,这样空字符串就会被映射为null,然后在对应的枚举属性中添加@NotNull注解就可以解决问题了。

import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.type.LogicalType;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObjectMapperConfig 
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() 
        return builder -> builder.postConfigurer(objectMapper -> 
            objectMapper.coercionConfigFor(LogicalType.Enum)
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
        );
    

【讨论】:

以上是关于如何处理枚举中空值的 InvalidFormatException的主要内容,如果未能解决你的问题,请参考以下文章

如何处理在 php mysql 中返回多个值的内部查询?

如何处理返回布尔值的 GraphQL 查询?

如何处理以数组作为返回值的 go RPC 调用?

如何处理具有 35 个唯一值的分类数据?

postgres 如何处理对某些列具有假值的视图的查询?

我们如何处理在表的所有记录中具有相同值的列?