For example, I have a list of some products in my cart like cinema ticket, carsharing and new book.
[
{
"name": "Cinema Ticket"
},
{
"name": "Car Sharing",
"properties": { ... }
},
{
"name": "New Book",
}
]
As you can see, not all products have properties
. Note: this field is polymorphic.
Can I turn "non-existing" field properties
into null
using Jackson or it's better to change api? And if I can then how to do that?
Jackson Version: 2.10.1
Thanks for answers!
I suppose you have something like:
public class BaseProduct {
public String name;
}
public class CarSharing extends BaseProduct {
public String properties;
}
public class Book extends BaseProduct {
}
Instead of having the properties field set to null when is not there, you can use Jackson polymorphic functionality to not show it. Something like:
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
public class BaseProduct {
public String name;
}
The output would be:
[
{
"type": ".Book",
"name": "Book name"
},
{
"type": ".CarSharing",
"name": "Car share name",
"properties": "a property field"
}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.