假设我有一些对象,例如带有地址对象列表的企业,以及包含企业的订单。
是否可以进行配置,以便在订单序列化时从业务对象中排除地址列表,而在业务序列化时包含该列表?
我正在使用 ajax 为 RIA 提取数据,在处理订单时我并不真正关心地址数据,但在处理业务时我确实需要列表。
我还使用 Hibernate 来实现持久性,因此这确实是一种效率和性能优化。
Say I have objects such as a Business with a List of Address objects, and an Order that has a Business.
Is it possible to configure so that when the Order is serialized it excludes the list of addresses from the Business object, and when the business is serialized it includes the list?
I'm using ajax to pull data for an RIA and when working with the Order I don't really care about the address data, but when dealing with Business I do want the list.
I'm also using Hibernate for persistence so this is really an efficiency and performance optimization.
发布评论
评论(4)
如果我正确理解问题,是的,我认为 Jackson 的 JSON 视图 会允许这样做。
您基本上会为同一类型创建两个不同的视图(配置文件),然后选择使用哪一个进行序列化。
If I understand question correctly, yes, I think JSON Views for Jackson would allow this.
You would basically create two different views (profiles) for same type, and choose which one to use for serialization.
您可以使用 JsonIgnore Annotation 来忽略序列化中的地址列表并关闭 ObjectMapper 的 SerializationConfig 序列化业务时。当然,这意味着您可能使用的其他注释也会被忽略。并不完美,但您可能会找到更好的解决方案来研究这个问题,希望它有帮助(显然)。
You could use the JsonIgnore Annotation to ignore the Address list in serialization and switch off the use of annotations in the ObjectMapper's SerializationConfig when serializing a Business. Of course, this means that other annotations you might use are ignored as well. Not perfect, but you might find a better solution looking into this, hope it helps (obviously).
是的,你可以做到。您所需要做的就是将地址列表声明为业务对象中的临时属性。
然后将以下代码添加到您的 jsonConfig 中:
Yes, you can do it. All you need is to declare the List of Address as transient property in you business object.
Then add the following code to your jsonConfig:
用于忽略您不想转换为 json 的属性。
输出:这将转换属性 id 和 documentUrl,但不会转换属性 documentType。
is used to ignore properties that you don't want to convert to json.
Output: This will convert the properties id and documentUrl but will not convert property documentType.