Spring中的MarshallingView调整输出?
我有一些 POJO,它们是我正在开发的 RESTful API 的基础。然而,我需要在一些响应中包含一些其他信息,以使 API 更加完整。我真的不想将这些额外的信息放在 POJO 中,而是将其包含在 Web 服务层中,就好像它是一样。
它涉及有“约会”的“人”。每个预约只有一个人。
因此,我有一个像 /Patients/1 这样的 RESTful 调用,它基本上获取了 Person 的 POJO,我目前正在使用 XStream 来序列化它并按其方式发送它。这很好用,但我想做这样的事情:
<Person>
<firstName>James</firstName>
... other fields ...
<nextAppointment href="/Appointment/12345>2010-02-19</nextAppointment>
<prevAppointment href="/Appointment/12346>2010-01-01</prevAppointemnt>
</Person>
下一个和上一个约会实际上并不包含在 Person POJO 中。我正在寻找一种好的“春天方式”来实现这一目标。客户端可以执行类似 /Patients/1/PreviousAppointment 和 /Patients/1/NextAppointment 的操作,但我希望减少呼叫量(也许是预优化?),并为他们提供一种在需要时获取更多信息的方法通过使用他href。
使用 XStreamMarshaller 非常优雅,因为我所做的一切都是将 POJO 或 POJO 列表的视图交给视图并由它处理。但在发出之前我需要对它们进行一些处理。
谢谢!
I have some POJOs which are the basis for this RESTful API I am working on. However, some of the responses I need to include some other information to make the API more complete. I really don't want to put these extra information in the POJO, but include it at the web service layer as if it were.
It deals with "People" who have "Appointments". Each appointment only has one person.
So, I have a RESTful call like /Patients/1 and it basically grabs the POJO for the Person and I am currently using XStream to serialize it and send it on its way. This works great, but I would like to do something like this:
<Person>
<firstName>James</firstName>
... other fields ...
<nextAppointment href="/Appointment/12345>2010-02-19</nextAppointment>
<prevAppointment href="/Appointment/12346>2010-01-01</prevAppointemnt>
</Person>
Where next and prev appointment are not actually included in the Person POJO. I am looking for a good "spring way" to accomplish this. The client could do something like this /Patients/1/PreviousAppointment and /Patients/1/NextAppointment, but I am looking to cut the amount of calls (maybe pre-optimization?) and give them a way to get more information if they need it by using he href.
It is very elegant using the XStreamMarshaller since all I do it hand the view the POJO or list of POJO and it handles it. But I need to doctors those up a bit before they are sent out.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是将业务对象直接交给编组器的问题 - 在他们如何将该对象转换为响应方面,您几乎没有灵活性。有话要说的是,您可以自己预先变换对象,这样您就可以获得更多控制权。
因此,如果您有想要的特定输出结构,那么使用 XStream,您需要构建一个与其类似的类结构。然后,您将业务对象转换为该类结构,并将其传递给 XStream。
它可能看起来不那么优雅,但是您的系统将不太容易被业务对象模型中的微小变化破坏,而您当前的基于 XStream 的系统将是这样。
This is the problem with handing your business objects directly to the marshaller - you have very little flexibility in how they turn that object into the response. There is something to be said for pre-transforming the objects yourself, you get more control that way.
So if you have a specific output structure that you want, then with XStream you need to build a class structure that looks like it. You then transform your business objects into that class structure, and pass that to XStream instead.
It may seem less elegant, but your system will be much less prone to being broken by small changes in your business object model, which you your current XStream-based system will be.
问题的解决方案:创建一个自定义转换器...
公共类自定义转换器实现转换器{
@Override
公共无效编组(对象源,HierarchicalStreamWriter writer,MarshallingContext context){ ....}
@Override
公共对象 unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {..}
@Override
public boolean canConvert(Class clazz) {..}
}
要了解如何将转换器与 Marshaller 一起使用,请参阅 这个。
所以基本上 CONVERTER 在 POJO 上工作并确保我们获得合同中给出的 XML 响应。
Solution to your problem : CREATE A CUSTOMIZEDCONVERTER...
public class CustomizedConverter implements Converter {
@Override
public void marshal(Object source, HierarchicalStreamWriter writer,MarshallingContext context) { ....}
@Override
public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {..}
@Override
public boolean canConvert(Class clazz) {..}
}
To know what to use the converter with the Marshaller refer this.
So basically the CONVERTER works on the POJO and ensures we get the XML response as given in the contract.