无法通过Jackson2ObjectMapperBuilder将空字符串设置为null DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

时间:2016-12-21 21:45:01

标签: java spring rest spring-boot jackson

问题

我正在尝试JacksonEmpty String设置为null这是Spring-boot应用程序中的@Size(min = 1, max = 255) @Column(length = 255) private String businessTitle; 。我们已经为POJO添加了验证,以检查该值是否在1到255 IFF之间。

Empty String

问题在于,当javax.validation.ConstraintViolationException传递给验证时,它将失败并出现DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT异常,因为它正在检查空字符串。

我尝试在Jackson2ObjectMapperBuilder上设置Empty String,因此它会将这些空字符串设置为null,并且验证不会失败。但这并没有发生,它继续将save()传递给对象的Empty String

问题

在保存Ojbject时,出现了什么问题或需要调整以使null正确设置为@Size(min = 1, max = 255) @Column(length = 255) private String businessTitle;

代码

对象

@Configuration
public class JacksonConfiguration {


  @Bean
  public Jackson2ObjectMapperBuilder filteringObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    builder.featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

    return builder;
  }
}

JacksonConfiguration

Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [com..model.User] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='size must be between 1 and 255', propertyPath=businessTitle, rootBeanClass=class com..model.User, messageTemplate='{javax.validation.constraints.Size.message}'}
]
        at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:138)
        at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:86)
        at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:244)
        at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:118)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456)
        at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
        at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465)
        at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963)
        at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339)
        at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
        at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
        at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
        at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61)
        ... 127 common frames omitted

异常

load_table_from_storage

2 个答案:

答案 0 :(得分:2)

DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT不会对你有所帮助,因为它意味着“将对象字段的空字符串JSON值反序列化为空”,不幸的是你的字符串字段是标量而不是JSON对象。 我认为没有方便的方法来获得你想要的行为。可以覆盖类型为String的所有字段的deserializaton行为,但您可能不需要它。您也可以定义自定义反序列化器,但是您需要注释每个字段:

@JsonDeserialize(using = MyStringDeseralizer.class)
private String businessTitle;

其中class MyStringDeseralizer extends JsonDeserializer<String>并实现您需要的转换逻辑。

答案 1 :(得分:0)

实际上有一个更好的解决方案,然后必须把它放在每个属性上。创建自定义反序列化器if Err.Number<>0 then MsgBox "ERROR: " Exit Do End If ,将''更改为null。这是基于同事解决方案。

EmptyToNullStringDeserializer