views:

363

answers:

2

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!

+1  A: 

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.

skaffman
This makes sense. So I think I basically have two options, either generate the XML myself using the various business objects I have, or create a new object which represents the output format I want to have and use XStream on that.The problem with the new objects is that it will require a bunch of glue code and a duplication of most of the fields from the actual POJO objects. I'm now just thinking about letting client gather the extra information via additional API calls until it is proven that performance is unacceptable.
jr
A: 

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.

abhishek dutta