我目前正在从纯 xml 转换许多 Web 服务。我已经将它们加载到soapUI 中,创建了一个客户端端口和模拟服务,并且请求响应运行良好。所以现在我正在尝试将 xml 复杂类型对象创建/转换为 java,但我失败了。
在soapUI中我有这个作为回应:
<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>
知道如何将其转换为 java 对象吗?查看原始 xml 有一个包含字段消息和结果的父对象,但我不明白版本部分。
这个怎么样?
<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>
我已经有一个工作的 Jaxb 客户端,我以前使用过并且已经在工作,但不知道如何调用上述示例。
我有:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}resultatImage>
似乎响应没有映射到我创建的 ResultatImage 类。
有什么想法吗?
我如何发送请求和解组响应。
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();
}
}
}
}
com.ipiel.response 包具有类 Resultat 和 ResultatImage,均标记为 @XmlRootElement。它还包含标记为 @XmlRegistry 的 ObjectFactory
谢谢,
czetsuya