javajaxbwadl

Having trouble unmarshalling a WADL


I need to query some endpoints for their WADLs, and test all the endpoints contained in the WADL. I'm using JaxB to unmarshal the WADL into POJOs (tried Jackson too, but got another error).

Here's the beginning of the XML:

<application>
  <grammars/>
      <resources base="https://gatewaydsapdev1.company.com/v2">
    <resource path="/contents">
      <resource path="/labels">
        <method name="GET">
          <request>
            <param name="include" style="query" type="string"/>
            <param name="q" style="query" type="string"/>
            <param name="offset" style="query" type="string"/>
            <param name="limit" style="query" type="string"/>
            <param name="flush" style="query" type="boolean"/>
          </request>
          <response>
            <representation element="Response" mediaType="application/json;charset=utf-8"/>
          </response>
        </method>
      </resource>
      ....

And here's the unmarshalling code:

JAXBContext jaxbContext = JAXBContext.newInstance(WadlApplication.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(xml);
WadlApplication application = (WadlApplication) unmarshaller.unmarshal(reader);

But I get this exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"application"). Expected elements are <{http://wadl.dev.java.net/2009/02}application>,<{http://wadl.dev.java.net/2009/02}doc>,<{http://wadl.dev.java.net/2009/02}grammars>,<{http://wadl.dev.java.net/2009/02}include>,<{http://wadl.dev.java.net/2009/02}link>,<{http://wadl.dev.java.net/2009/02}method>,<{http://wadl.dev.java.net/2009/02}option>,<{http://wadl.dev.java.net/2009/02}param>,<{http://wadl.dev.java.net/2009/02}representation>,<{http://wadl.dev.java.net/2009/02}request>,<{http://wadl.dev.java.net/2009/02}resource>,<{http://wadl.dev.java.net/2009/02}resource_type>,<{http://wadl.dev.java.net/2009/02}resources>,<{http://wadl.dev.java.net/2009/02}response>

I don't know how to give the ObjectFactory the data it says is missing. Does anyone have any suggestions? TIA!


Solution

  • Your XML does not have a namespace whereas your JAXB classes do have the namespace http://wadl.dev.java.net/2009/02.

    Either add namespace to the XML or drop it from your JAXB classes.