jackson @JsonIgnoreProperties不会忽略'springframework.validation..中隐含的错误

brqmpdu1  于 2022-11-08  发布在  Spring
关注(0)|答案(1)|浏览(135)

我有一个用grails.validation.Validateable注解的对象,它将一个errors对象注入到它所注解的对象中。然后我尝试用com.fasterxml.jackson.databind.ObjectMapper.readValue(myJson)反序列化这个对象。
Cannot construct instance of org.springframework.validation.Errors (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
即使我在上面使用了@JsonIgnoreProperties(["errors", "Errors"])ignoreUnknown = true
我假设这是因为错误作为类成员是不可见的,但不知何故沿着被注入了?因此,也许JsonIgnoreProperties不知道它,并不确保它被忽略?
我甚至尝试将Errors errors成员添加到类中,并将@JsonIgnore添加到类中,试图将隐式错误显式化并忽略它,但这并没有改变什么。
如果我不想在objectmapper上使用全局忽略属性,这里有什么建议?

vm0i2vca

vm0i2vca1#

用这里的顶行修正了它:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonIgnoreProperties(value = ["errors"], allowGetters = true)
@Validateable
public class SomeClass {}

AutoDetect注解不知何故发现了隐藏的“errors”属性。我在那里使用了allowGetter,因为我想序列化它们,而不是反序列化它们。尽管必须构建自定义序列化程序来实现这一点。但是,您可以只删除allowGetters,而根本不处理错误。

相关问题