Can you serialize object's property instead of a whole object in Spring REST? (object being a field of a class)

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

Can you serialize object's property instead of a whole object in Spring REST? (object being a field of a class)

问题

为了澄清 - 我在一个Spring REST项目中有一个类,其中一个字段是另一个类的实例。

public class Event {

...
...
...

private Location location;
}

location 对象有一个类型为 intid 字段。因此,在序列化 Event 对象时,是否可以以某种方式只附加 location.id 字段,而不是整个序列化的 location 对象?

英文:

To clarify - I have some class in a Spring REST project which has an instance of some other class as one of its fields.

public class Event {

...
...
...

private Location location;
}

The location object has some id of type int. So when serializing Event object is it possible to somehow only attach the location.id field instead of the whole serialized location object?

答案1

得分: 3

If you are allowed to modify Location class, you can try @JsonValue annotation on the Location.id field:

public class Event {
    private String someField;
    private Location location;
}

public class Location {
    @JsonValue
    private int id;
}

It will get serialized to:

{
  "someField": "someValue",
  "location": 122
}

If you prefer a different name for the serialized field, use @JsonProperty:

public class Event {
    private String someField;

    @JsonProperty("locationId")
    private Location location;
}

public class Location {
    @JsonValue
    private int id;
}
{
  "someField": "someValue",
  "locationId": 122
}

Another option would be using @JsonUnwrapped in conjunction with @JsonIgnoreProperties. The Location class doesn't need to be changed in this case, but all properties to be excluded need to be explicitly listed.

public static class Event {
    private String someField;

    @JsonUnwrapped(prefix = "location_")
    @JsonIgnoreProperties({"fieldA", "fieldB", ...})
    private Location location;

}
{
  "someField": "someValue",
  "location_id": 122
}

Please note that both solutions will break deserialization.

英文:

If you are allowed to modify Location class, you can try @JsonValue annotation on the Location.id field:

public class Event {
    private String someField;
    private Location location;
}

public class Location {
    @JsonValue
    private int id;
}

It will get serilized to:

{
  "someFiled": "someValue",
  "location": 122
}

If you prefer different name of the serialized field, use @JsonProperty:

public class Event {
    private String someField;

    @JsonProperty("locationId")
    private Location location;
}

public class Location {
    @JsonValue
    private int id;
}
{
  "someFiled": "someValue",
  "locationId": 122
}

Another option I can think of would be using @JsonUnwrapped in conjunction with @JsonIgnoreProperties. The Location class doesn't need to be changed in this case, but all properties to be excluded need to be explicitly listed.

public static class Event {
    private String someField;

    @JsonUnwrapped(prefix = "location_")
    @JsonIgnoreProperties({"fieldA", "fieldB", ...})
    private Location location;

}
{
  "someFiled": "someValue",
  "location_id": 122
}

> Bare in mind that both solutions will break deserialization.

答案2

得分: 1

如果您序列化整个Event对象,我认为不可能避免序列化位置对象,因为它是其属性之一。但是,您可以尝试序列化对象的属性而不是整个对象本身,这样您可以序列化location.id而不是整个location对象。

唯一的问题是,您需要找到一种方式来在序列化到文件中组织这些数据,或者只为每个属性创建一个不同的文件。

英文:

If you serialize the whole Event object I believe it is not possible to avoid serializing the location object too as it is one of its properties. You could, however, attempt to serialize the properties of the object and not the object itself, in which case you could serialize location.id and not the whole location object.

The only problem is that you would have to find some way to organize this data in the file that you serialize it to, or just create a different file for each property.

huangapple
  • 本文由 发表于 2020年8月11日 02:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346077.html
匿名

发表评论

匿名网友

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

确定