Jackson's Access.WRITE_ONLY during test null

前端 未结 3 1868
栀梦
栀梦 2021-01-15 01:21

I\'m currently playing around with Jackson\'s de/serialization features and I encountered a problem, I don\'t know how to solve.

During my test the @JsonProper

相关标签:
3条回答
  • 2021-01-15 01:27

    I had the same problem, with a similar setup. The problem is in the test input data. Basically, writeValueAsBytes() will ignore the password while serializing exactly as instructed by the annotation.

    Note that, Access.WRITE_ONLY basically means "SetterOnly" or "DeserializationOnly" not the other way around.

    0 讨论(0)
  • 2021-01-15 01:41

    add the line for your ObjectMapper:

    mapper.disable(MapperFeature.USE_ANNOTATIONS);
    
    0 讨论(0)
  • 2021-01-15 01:47

    Ignoring all the annotations can be problematic. To handle a finer configuration you can implement your custom JacksonAnnotationIntrospector:

    public class IgnoreJacksonWriteOnlyAccess extends JacksonAnnotationIntrospector {
    
        @Override
        public JsonProperty.Access findPropertyAccess(Annotated m) {
            JsonProperty.Access access = super.findPropertyAccess(m);
            if (access == JsonProperty.Access.WRITE_ONLY) {
                return JsonProperty.Access.AUTO;
            }
            return access;
        }
    }
    

    Then, after instantiating the mapper:

    mapper.setAnnotationIntrospector(new IgnoreJacksonWriteOnlyAccess());
    
    0 讨论(0)
提交回复
热议问题