问题
Given an application.wadl
file, how do I generate Client app (Spring or any) and domain objects from a wadl file?
I tried :
wadl2java https://genologics.com/files/permanent/API/2.5/application.wadl
WADLToJava Error: java.lang.IllegalStateException: Single WADL resources element is expected
回答1:
This is my findings by reviewing the source-code:
As SourceGenerator.java, wadltojava is trying to get the "resources" element from the "application" element and expects it to be one only.
private void generateResourceClasses(Application app, GrammarInfo gInfo,
Set<String> typeClassNames, File src) {
Element appElement = app.getAppElement();
List<Element> resourcesEls = getWadlElements(appElement, "resources");
if (resourcesEls.size() != 1) {
throw new IllegalStateException("Single WADL resources element is expected");
}
List<Element> resourceEls = getWadlElements(resourcesEls.get(0), "resource");
if (resourceEls.size() == 0) {
throw new IllegalStateException("WADL has no resource elements");
}
........
}
I checked the WADL you provided and seems like there is only one "resources" element.
On checking further in getWadlElements()
method is using getWadlNamespace()
:
private List<Element> getWadlElements(Element parent, String name) {
List<Element> elements = parent != null
? DOMUtils.getChildrenWithName(parent, getWadlNamespace(), name)
: CastUtils.cast(Collections.emptyList(), Element.class);
if (!"resource".equals(name)) {
for (int i = 0; i < elements.size(); i++) {
Element el = elements.get(i);
Element realEl = getWadlElement(el);
if (el != realEl) {
elements.set(i, realEl);
}
}
}
return elements;
}
The namespace used here in WadlGenerator.java is
public static final String WADL_NS = "http://wadl.dev.java.net/2009/02";
But in your WADL the namespace seems to be different as below, and may be causing issue.
<wadl:application xmlns:wadl="http://research.sun.com/wadl/2006/10" xmlns:xs="http://www.w3.org/2001/XMLSchema">
It seems that you are using CXF so as per my understanding, I would suggest you to use the same framework which is used to generate the WADL.
Update: Or, have the WADL and XSD's on your local and modify the namespace manually in WADL to the latest one and try again.
来源:https://stackoverflow.com/questions/31492866/how-to-create-a-client-app-for-a-restful-service-from-wadl