I don't expect this will be a tough question, but with all that Jackson has to offer, I hope there is a solution that will work for me. Am using Java 11 now.
Let's say I have an object that looks like this:
public class MyMainObject() {
private Long id;
private String myName;
private SomeObject someObject;
... getters/setters
}
public class SomeObject() {
private Long someId;
private String someName;
}
Now, I know I can use Jackson to convert this code as follows to json:
MyMainObject myMainObject = new MyMainObject();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myMainObject);
I know that this will give me the entirety of 'SomeObject' in the JSON, which is fine. But now, I just want to get the 'someId' field from 'SomeObject'.
I am wondering if there is some Jackson Annotation I could use in the 'MyMainObject' with the field 'someObject' so I only get the 'someId' from 'SomeObject'.
I suppose I could forget this and just do a mapping from these two objects to some other DTO. Or, I could put @JsonIgnore for the SomeObject.someName and that would work also. But in this case, imagine SomeObject has many more fields than the ones I listed, and I don't want to use @JsonIgnore for all those fields.
So, if there is a quick Jackson Annotation I could use to just get the one field from the SomeObject, that would be great. I'll keep checking the Jackson Annotations to see if there is something.