Hi
I would like to allow the user to somehow specify which elements they
care about for an object, so that the output can be optimized as well as
the queries backing the service.
It shoud be possible to do with JAX-RS.
For instance, if a call is made to retrieve a Contact and the user
specifies "firstName,lastName", I only want the output to contain those
fields. Secondarily, getting rid of empty elements would be nice. This
could be via a URL for instance:
/contact/2?fields=firstName,lastName
Sure, depends on how you'd like to do it. One way is to deal with it explicitly
in the application code by checking the query
parameters and acting accordingly. Alternatively you can do a post-processing,
If you're not after using a servlet filter then
another option to rely on a JAX-RS MessageBodyWriter.
Eg.
@Path("/contacts")
@ProduceMime("application/xml")
public class Contacts {
@GET
public Contacts getContact(@PathParam int id) {
retrun new StreamSource(...);
}
}
class ContactsWriter implements MessageBodyWriter<Contacts> {
private MultivaluedMap<String, String> currentQuery;
@Context
public void setUriInfo(UriInfo ui) {
currentQuery = ui.getQueryparameters();
}
void writeTo(Contacts cs, Class<Contacts> clazz, ..., OutputStream os) {
// write start tag
for (Contact c : cs) {
if (c.getName().equals(currentQuery.getFirst("firstName"))) {
// write
}
}
// write end tag
}
}
There could be other options available, depending on how you'd like to do it.
Did I understand your question correctly ?
Also it's possible to have the same method's response be serialized as json or
xml, etc, depending on the value of Accept, it's a
standard JAX-RS feature
Cheers, Sergey
Does CXF provide any mechanism for doing the above? If there is no
mechanism for performing the dynamic selection of elements, a
configuration for eliminating empty elements in output would at least
allow me to easily create a servlet filter/interceptor that got the job
done.
Thanks in advance.
Cheers,
David
----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland