i am trying to create a REST service that accepts a List of objects from a client and gives back a zip file.
i understand how to give back the zip file alright.
But i am right now trying to figure out a way i can pass a List of objects from a REST client/browser to the Rest service and how do i accept the List in the REST service.
Should this be done via XML input ?
or maybe the @consumes annotation could help?
Much thanks .
Som
You need to think out more clearly what you wish to do. There’s no really good reason for taking a list of objects and returning a ZIP of them; you might as well use a local zip program (which just about all computers already have). That indicates that we instead need to be looking at something sensible: for example, a list of names of objects that you’ll return a ZIP of, that makes a lot of sense. There are other sensible things you could be doing too, but you have to work it out in your mind what you want to happen.
Because you mention “@consumes annotation”, I’m going to assume you’re using JAX-RS (i.e., Java). That’s nice, because it’s entirely possible to do on-the-fly ZIP generation with that; the content type you want to produce is
application/zip. The easiest way I’ve found to handle the specification of the list of descriptions of things to return is as a wrapped list, where you use something like JAXB to do the mapping (which gives you XML support; some frameworks also support JSON off the same data models). To do a wrapped list, you use something like this:That then produces/handles XML documents like this (a three item list):
You’ll need to set up the
@Consumesannotation so that the content type accepted isapplication/xml(at least), and also consider what type of operation is involved and on what resource.[EDIT]: In order to create a REST service that takes a list of strings as arguments, the easiest method is indeed to use a wrapper object, much as above. (You can’t take a raw list; it needs to be a well-formed XML document when it’s on the wire.) We then set up the annotated service method like this:
The key is that the
reqargument (the name is arbitrary, of course) is the only argument that is not annotated, that it is of a type that is JAXB-enabled, and there is an overall@Consumes ("application/xml")annotation to enable the JAXB processing of the request body. (I handle the returning of a ZIP by generating theResponsedirectly rather than relying on the framework to do the processing for me; this lets me control the content type handling a little more precisely.)Also note that some frameworks can also transfer JAXB-annotated objects as JSON documents, just by having a bit of extra annotation; you just state that the method can accept both "
application/xml" and "application/json" in the@Consumesannotation. I do not know whether this applies to the framework you are using (I’ve only tested it with Apache CXF).