jsongrailsjacksonspring-validator

@JsonIgnoreProperties does not ignore `springframework.validation.Errors` that are implicit in the @Validateable object


I have an object that is annotated with grails.validation.Validateable and that injects an errors object into whatever it annotates. Then I am trying to deserialize this object with com.fasterxml.jackson.databind.ObjectMapper.readValue(myJson). However, it fails with

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

even though I used @JsonIgnoreProperties(["errors", "Errors"]) on it or ignoreUnknown = true as well.

I am assuming this is because the errors is not visible as a class member, but is somehow injected along the way? And thus, maybe JsonIgnoreProperties does not know about it and does not ensure it being ignored?

I even tried adding Errors errors member into the class and added @JsonIgnore to it, in an attempt to make the implicit errors explicit and ignore it like that, but it didn't change a thing.

What would be recommended here, if I don't want to go for global ignore properties on the objectmapper?


Solution

  • Fixed it with the top line here:

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

    The AutoDetect annotation somehow finds out about the hidden "errors" property. I used allowGetters there, because I wanted to serialize them, but not deserialize them. Custom serializer had to built to achieve that though. You could however just remove allowGetters and not deal with errors at all.