2

I have ObjectMapper instance:

ObjectMapper mapper = new ObjectMapper(); 

In runtime want to serialize instance of class. What is the class the program doesn't known. It's object instance of parameterized type T.

How to ignore all properties (fields and getters) which marked specified annotation (javax.persistence.Id) ?

Example:

public static class PojoTest { @Id public String idTest; public String id; } public void serialize(Object object) { ObjectMapper objectMapper = new ObjectMapper(); // TODO ignore property mark @Id annotation Map<Object, Object> map = objectMapper.convertValue(object, Map.class); assertFalse(map.containsKey("idTest")); } public void test() { PojoTest pojoTest = new PojoTest(); pojoTest.id = "foo"; pojoTest.idTest = "bar"; serialize(pojoTest); } 
3
  • You can mark them transient Commented Jul 6, 2020 at 20:02
  • You also add @JsonIgnore to those fields. Commented Jul 6, 2020 at 20:03
  • This is not exactly what I need. See example Commented Jul 6, 2020 at 20:07

1 Answer 1

3

You can implement a new com.fasterxml.jackson.databind.AnnotationIntrospector class where you can extend hasIgnoreMarker method:

static class IdIgnoreAnnotationIntrospector extends AnnotationIntrospector { @Override public Version version() { return new Version(1,0,0,"Ignore @Id", "group.id", "artifact.id"); } @Override public boolean hasIgnoreMarker(AnnotatedMember m) { return hasIdAnnotation(m); } boolean hasIdAnnotation(AnnotatedMember member) { return member.getAnnotation(Id.class) != null; } } 

Now you need to register this introspector:

ObjectMapper mapper = new ObjectMapper(); mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(new JacksonAnnotationIntrospector(), new IdIgnoreAnnotationIntrospector())); 

Now you can ignore all fields marked with @Id annotation.

Sign up to request clarification or add additional context in comments.

5 Comments

not working for me for some reason.
@Mr_Hmp, are you sure you have configured properly IdIgnoreAnnotationIntrospector? Modern complex web apps could have more than one ObjectMapper.
Very elegant example for re-using the AnnotationIntrospector to ignore JSON-properties based on some foreign (different-purpose) annotations💡️. Would you advice to consider the extension-warning in linked JavaDocs, or can it be ignored in simple cases like here?
@hc_dev, do you mean a note where it is suggested to use NopAnnotationIntrospector class instead of AnnotationIntrospector? In example like this it is important to show the simplest possible solution without overcomplicating it. Most developers probably would notice it and use but to make it better I will update later the answer.
Yes, I was just asking about your opinion on that note. Now I see, you considered it and deliberately decided to use a simple example. That's wise and fine teacher's practice. Then don't over-complicate your answer - a short warning-note suffices IMO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.