I am struggling for a couple of days now with the following problem. I searched quite a lot for an answer, here in SO, in jersey mailing lists and the net in general, but weren't able to find answer to this particular question.
Setting up the problem domain...
I am using Jersey 1.16 inside Tomcat 7.
I have created a simple JAX-RS resource looking like this:
@Path("/")
@Produces({ "application/xml", "text/plain" })
public class ExampleResource {
@GET
public List<Thing> getThings() {
List<Thing> list = new ArrayList<>();
list.add(new Thing("a thing 1", "a thing description 1"));
list.add(new Thing("a thing 2", "a thing description 2"));
return list;
}
}
Thing
is a JAXB annotated POJO looking like this
@XmlRootElement(name = "thing")
public class Thing {
private String name;
private String description;
// getters, setters and @XmlElement annotations ommited for brevity
I have also configured WadlGeneratorJAXBGrammarGenerator.class
And when I ask for GET http://localhost:8092/rest
it works like a charm - nicely formatted collection of Thing
is returned.
The automatically generated WADL http://localhost:8092/rest/application.wadl
is almost perfect, it looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.16 11/28/2012 02:09 PM" />
<grammars>
<include href="application.wadl/xsd0.xsd">
<doc title="Generated" xml:lang="en" />
</include>
</grammars>
<resources base="http://localhost:8092/rest/">
<resource path="/">
<method id="getThings" name="GET">
<response>
<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02"
xmlns="" element="thing" mediaType="application/xml" />
<representation mediaType="text/plain" />
</response>
</method>
</resource>
</resources>
</application>
Like I said, almost perfect, and therein lies the problem.
<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02"
xmlns="" element="thing" mediaType="application/xml" />
The WADL is not describing that /getThings
returns a List<Thing>
.
Rather, it looks like it is referring to a single element thing
in the xsd0.xsd
.
So, when I feed it in e.g. wadl2java, it generates untyped client.
In order to get a List<Thing>
I have to manually code it, something like
List<Thing> asXml = root().getAsXml(new GenericType<List<Thing>>(){});
Does anyone know if it is possible to have automatic WADL generation that would somehow indicate that this particular resource is returning a List of resources of a specific type?
And I don't want to create additional "ThingList" JAXB-annotated class and return that instead in my jersey resource.
I am almost there with generating the "perfect" WADL, it is just this (hopefully) little piece that I am missing...
Thank you very much!
Person
) becomes axs:complexType
with one element for each property it has. A property, sayString name
, looks like<xs:element minOccurs='0' name='name' nillable='true' type='xs:string'/>
(theminOccurs='0'
implies that is is an optional field). A property that is a list, sayString[] nicknames
would become something like:<xs:element maxOccurs='unbounded' minOccurs='0' name='nicknames' nillable='true' type='xs:string'/>
. As you can see, the only difference from a simple field to a list ismaxOccurs='unbounded'
. – Conspiracy