使用Jackson从子对象中获取一个字段。

huangapple go评论71阅读模式
英文:

Use Jackson to get one Field from sub-object

问题

我不认为这会是一个困难的问题,但考虑到Jackson提供了许多功能,我希望有一个适合我的解决方案。我现在正在使用Java 11。

假设我有一个如下所示的对象:

public class MyMainObject() {
    private Long id;
    private String myName;
    private SomeObject someObject;
    ... getters/setters
}

public class SomeObject() {
    private Long someId;
    private String someName;
}

现在,我知道可以使用Jackson将此代码转换为JSON,如下所示:

MyMainObject myMainObject = new MyMainObject();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myMainObject);

我知道这将在JSON中给我整个'SomeObject',这没问题。但现在,我只想获取'SomeObject'中的'someId'字段。

我想知道是否可以在'MyMainObject'中使用某种Jackson注释来仅获取'SomeObject'中的'someId'字段。

我可以选择忽略这个问题,只需将这两个对象映射到其他DTO。或者,我可以为'SomeObject.someName'添加@JsonIgnore注释,这也可以工作。但在这种情况下,想象一下'SomeObject'有比我列出的更多字段,我不想为所有这些字段都使用@JsonIgnore。

因此,如果有一个快速的Jackson注释可以让我从SomeObject中获取一个字段,那将是很好的。我将继续查看Jackson注释,看看是否有相关的内容。

英文:

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.

答案1

得分: 2

If I understand your query correctly, you'd like to include just a single property of SomeObject when serializing it using Jackson. If that's correct, then I think @JsonIncludeProperties class annotation might be a good fit. Something along these lines:

@JsonIncludeProperties({"someId"})
public class SomeObject {
    private Long someId;
    private String someName;
    // other properties to be ignored when serializing
    // getters/setters
}

The output result would not include other properties of SomeObject in the serialization (only the property/properties specified by @JsonIncludeProperties):

{"id":null,"myName":null,"someObject":{"someId":123}}

It was added in Jackson Release 2.12: Jackson Release 2.12

Additional details linked from the GitHub release post: GitHub Release Post

英文:

If I understand you query correctly, you'd like to include just a single property of SomeObject when serializing it using Jackson. If that's correct, then I think @JsonIncludeProperties class annotation might be a good fit. Something along these lines:

@JsonIncludeProperties({"someId"})
public class SomeObject {
    private Long someId;
    private String someName;
// other properties to be ignored when serializing
...
// getters/setters

}

The output result would not include other properties of SomeObject in the serialization (only the property/properties specified by @JsonIncludeProperties):

{"id":null,"myName":null,"someObject":{"someId":123}}

It was added in Jackson Release 2.12: https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.12#jsonincludeproperties

Additional details linked from the GitHub release post: https://cowtowncoder.medium.com/jackson-2-12-most-wanted-2-5-bc45ef53ede7

huangapple
  • 本文由 发表于 2023年6月13日 05:04:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定