使用 JAXB 进行灵活编组
我希望有一种灵活的方式来编组对象。单个对象的详细版本和多个对象版本的不太详细版本。
例如,考虑我的部门模型:
GET /locations/1:
<location id='1'>
<link rel="self" href="http://example.com/locations/1"/>
<link rel="parent" href="http://example.com/serviceareas/1"/>
<name>location 01</name>
<departments>
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<name>department 01</name>
</department>
<department id='2'>
<link rel="self" href="http://example.com/departments/2"/>
<name>department 02</name>
</department>
<department id='3'>
<link rel="self" href="http://example.com/departments/3"/>
<name>department 03</name>
</department>
</departments>
</location>
GET /department/1:
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<link rel="parent" href="http://example.com/locations/1"/>
<name>department 01</name>
<abbr>dept 01</abbr>
....
<specialty>critical care</specialty>
</department>
有没有办法做到这一点?我需要有单独的实体对象吗?一个引用表进行 CRUD 操作,另一个引用列表?
I'm hoping to have a flexible way of marshalling objects. A verbose version for single objects and a less-verbose version for multiple object versions.
For example, consider my department model:
GET /locations/1:
<location id='1'>
<link rel="self" href="http://example.com/locations/1"/>
<link rel="parent" href="http://example.com/serviceareas/1"/>
<name>location 01</name>
<departments>
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<name>department 01</name>
</department>
<department id='2'>
<link rel="self" href="http://example.com/departments/2"/>
<name>department 02</name>
</department>
<department id='3'>
<link rel="self" href="http://example.com/departments/3"/>
<name>department 03</name>
</department>
</departments>
</location>
GET /department/1:
<department id='1'>
<link rel="self" href="http://example.com/departments/1"/>
<link rel="parent" href="http://example.com/locations/1"/>
<name>department 01</name>
<abbr>dept 01</abbr>
....
<specialty>critical care</specialty>
</department>
Is there a way to do this? Would I need to have separate entity objects? One that references the table for CRUD operations and another for lists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:我是EclipseLink JAXB (MOXy) 领导者,以及 JAXB 2 成员(JSR-222)专家组。
您的问题被标记为
EclipseLink
,如果您使用 EclipseLink JAXB (MOXy),您可以利用外部绑定文档将第二个映射应用到Department< /代码> 类。
ContextResolver
在 JAX-RS 环境中,您可以通过
ContextResolver
利用 MOXy 的外部绑定文档:了解更多信息
外部绑定文档
默认 MOXy 的外部绑定文档用于增强带注释的模型,但如果您设置 xml-mapping-metadata-complete 标志,它将完全覆盖注释,从而允许您应用完全不同的映射:
了解更多信息
更新
此更新是为了解决您在评论中提出的一些问题:
是的,每个 ContextResolver 都应该有自己的绑定文件。引入新的 ContextResolver 的主要原因是表示辅助映射。
对于单个 ContextResolver,您可以跨多个绑定文件表达元数据,但它们将被组合成一组映射。这意味着单个
ContentResolver
不能拥有单个类的多个视图。单独的 ContextResolver 用于表示辅助映射。我建议从类路径加载元数据文件。
您的 JAX-RS 实现应该选择您的
ContextResolver
,因为它是用@Provider
注释的。用于类的 ContextResolver 取决于您如何实现 getContext 方法:Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
Your question is tagged
EclipseLink
, if you are using EclipseLink JAXB (MOXy) you can take advantage of the external binding document to apply a second mapping to theDepartment
class.ContextResolver
In a JAX-RS environment you can leverage MOXy's external binding document through a
ContextResolver
:For More Information
External Binding Document
By default MOXy's external binding document is used to augment the annotated model, but if you set the
xml-mapping-metadata-complete
flag it will completely override the annotations allowing you to apply a completely different mapping:For More Information
UPDATE
This update is to address a number of questions you asked in one of your comments:
Yes each
ContextResolver
should have its own binding file. The main reason for introducing a newContextResolver
is to represent a secondary mapping.For a single
ContextResolver
you can express the metadata across multiple binding files, but they will be combined into a single set of mappings. This means that a singleContentResolver
cannot have multiple views of a single class. A separateContextResolver
is used to represent a secondary mapping.I recommend loading the metadata file from the class path.
Your JAX-RS implementation should pick up your
ContextResolver
because it is annotated with@Provider
. TheContextResolver
used for a class will depend on how you implement thegetContext
method:这是另一个想法。可能是一个坏主意,但有点简单。
Here comes another idea. Could be a bad idea but somewhat easy.