I’m currently converting a number of web services from plain xml. I’ve load them in soapUI, created a client port and mock service and the request response is working perfectly. So right now I’m trying to create/convert the xml complex type objects into java, but I’m failing.
In soapUI I have this as a response:
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://client.serviceweb.xxx">
<soapenv:Header/>
<soapenv:Body>
<cli:versionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<result xsi:type="java:ResultatVersion" xmlns:java="java:xxx.serviceweb">
<messagexxx xsi:type="xsd:string">message</messagexxx>
<resultatxxx xsi:type="xsd:int">1</resultatxxx>
<version xsi:type="java:Version">
<numero xsi:type="xsd:string">1.0.0</numero>
</version>
</result>
</cli:versionResponse>
</soapenv:Body>
</soapenv:Envelope>
Any idea how to convert that to a java object? Looking at the original xml there’s a parent object that contains the field message and result, but I don’t understand the version part.
How about this one?
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xxx="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://client.serviceweb.xxx">
<soapenv:Header/>
<soapenv:Body>
<cli:testResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<result xsi:type="java:ResultatImage" xmlns:java="java:xxx.serviceweb.xxx.xxx">
<messagexxx xsi:type="xxx:string">message</messagexxx>
<resultatxxx xsi:type="xxx:int">result</resultatxxx>
<image xsi:type="xxx:base64Binary">cid:1325182441595</image>
</result>
</cli:testResponse>
</soapenv:Body>
</soapenv:Envelope>
I already have a working Jaxb client that I have used previously and is already working, but can’t figure out how to call the above samples.
I’m having:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}resultatImage>
Seems like the response is not mapping to the ResultatImage class that I’ve created.
Any thoughts?
How I send the request and unmarshall the response.
try {
JAXBContext jc = JAXBContext
.newInstance("com.ipiel.response");
Unmarshaller u = jc.createUnmarshaller();
client = new MyClient(httpClient, targetHost, u);
client.test();
} catch (Exception e) {
e.printStackTrace();
}
public void test() {
String url = "/xxxClientsPort/test";
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(targetHost, httpPost);
log.info("response: " + response);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity respEntity = response.getEntity();
if (respEntity != null) {
InputStream instream = respEntity.getContent();
try {
ResultatImage responseEntity = (ResultatImage) unmarshaller
.unmarshal(instream);
/*
* FileWriter writer = new FileWriter(new
* File("c:\\tmp\\output")); IOUtils.copy(instream, writer,
* "UTF-8"); String theString = writer.toString();
* writer.flush(); writer.close();
* System.out.println(theString);
*/
} finally {
instream.close();
}
}
}
}
package com.ipiel.response has the class Resultat and ResultatImage, both marked as @XmlRootElement. Also it contains the ObjectFactory marked as @XmlRegistry
Thanks,
czetsuya
The main problem here was that I was generating the java classes inside SoapUI using the wrong version of Axis. I was trying with Axis2, but later found out that the webservice is actually running on Axis1. So if you ever encounter a similar problem, check first if the webservice version and the library of your client is the same.