我参考下列资料
http://stackoverflow.com/questions/11465653/how-to-unmarshall-soap-response-using-jaxb-if-namespace-declaration-is-on-soap-e
http://stackoverflow.com/questions/11425109/how-to-include-the-soap-envelope-tag-when-marshalling-using-jaxb
http://cxf.apache.org/download.html
http://blog.youkuaiyun.com/zhigangsun/article/details/2698810
First use apache-cxf-2.5.6 to create java class code from WSDL
call .\tools\apache-cxf-2.5.6\bin\wsdl2java -b .\jaxb-bindings.xml -d .\gen .\src\wsdl\UBS_Investment_Suitability_Models.wsdl
jaxb-bindings.xml
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>
Those are supported third-parties library
activation.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.1.3.jar
stax2-api-3.1.1.jar
stax-api-1.0.jar
woodstox-core-asl-4.1.2.jar
We use Unmarshaller/Marshaller transfer between java object ( it is created by CXF from WSDL) and xml string.
The following is java code
public VRResponse ummarshaller(String xml) throws Exception {
JAXBContext jaxbCtx = JAXBContext.newInstance(VRResponse.class);
Unmarshaller ummarshaller = jaxbCtx.createUnmarshaller();
// marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
// Boolean.TRUE);
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
xsr.nextTag(); // Advance to Envelope tag
xsr.nextTag(); // Advance to Body tag
xsr.nextTag(); // Advance to getNumberResponse tag
System.out.println(xsr.getNamespaceContext().getNamespaceURI(
"vrresponse"));
JAXBElement<VRResponse> object = ummarshaller.unmarshal(xsr,
VRResponse.class);
return object.getValue();
}
public String marshaller(VRResponse response) throws Exception {
JAXBContext jaxbCtx = JAXBContext.newInstance(VRResponse.class);
Marshaller marshaller = jaxbCtx.createMarshaller();
StringWriter w = new StringWriter();
marshaller.marshal(response, w);
return w.getBuffer().toString();
}
public VRRequest loadRequest(String fileName) throws Exception {
JAXBContext jaxbCtx = JAXBContext.newInstance(VRRequest.class);
Unmarshaller ummarshaller = jaxbCtx.createUnmarshaller();
// marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
// Boolean.TRUE);
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(this
.getClass().getResource(fileName).getFile()));
xsr.nextTag(); // Advance to Envelope tag
xsr.nextTag(); // Advance to header
xsr.nextTag(); // Advance to Body tag
xsr.nextTag(); // Advance to getNumberResponse tag
System.out.println("serviceName="
+ xsr.getNamespaceContext().getNamespaceURI("ok"));
JAXBElement<VRRequest> object = ummarshaller.unmarshal(xsr,
VRRequest.class);
return object.getValue();
}
Here, I need point a issue.
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
xsr.nextTag(); // Advance to Envelope tag
xsr.nextTag(); // Advance to Body tag
xsr.nextTag(); // Advance to getNumberResponse tag
System.out.println(xsr.getNamespaceContext().getNamespaceURI(
"vrresponse"));
JAXBElement<VRResponse> object = ummarshaller.unmarshal(xsr,
VRResponse.class);
因为JAXB不能解析
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
例如,soap messge xml string is
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<vrresponse:VRResponse xmlns:vrresponse="http://www.ubs.execution-server.com/vrpath/UBS_Investment_Suitability_Models/executeInvestmentSuitability_OnPersonPlacingOrder/" xmlns:vr="http://www.visual-rules.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<output>
<CONTROL_ID>TRANSACTION_CONTROL</CONTROL_ID>
<BATCH_ID>199</BATCH_ID>
<RESPONSES>
<element>
<GDK_CHECK>OK</GDK_CHECK>
<SDK_RESULT>Undetermined</SDK_RESULT>
<GDK_RESULT>NA</GDK_RESULT>
<SDK_CHECK>NOK</SDK_CHECK>
<ALERT_MESSAGE>I confirm that :-<BR />* where available, the termsheet, prospectus/ offering circular have been made available to the client as permitted under the relevant Supplementary Instruction; and<BR />* the key risks and product features including economic terms with scenario analysis and margin requirements (if any) have been explained to the client (except for products not on UBS product shelf and such information is not available to UBS)<BR />* Non Default PRR: Assess and document investment rationale / justification e.g. product aligns with client target asset allocation, trade is small % of portfolio AUM etc<BR />* High(DF) (i.e. no PRR): Inform client that product is not rated by UBS. Obtain client acknowledgement of understanding of product features and risks and client makes own investment decision</ALERT_MESSAGE>
<GDK_PRODUCT_BRO_DES/>
<CS>None</CS>
<PSL>High</PSL>
<SM_CHECK>OK</SM_CHECK>
<SM_RESULT>NA</SM_RESULT>
<RM_CHECK>NOK</RM_CHECK>
<CRP2PRP>N</CRP2PRP>
<RM_RESULT>Mismatch</RM_RESULT>
<PRR>High(DF)</PRR>
<PRP>Medium</PRP>
<VCPPO_CHECK>OK</VCPPO_CHECK>
<VCPPO_RESULT>Undetermined</VCPPO_RESULT>
<VCBO_CHECK>OK</VCBO_CHECK>
<VCBO_RESULT>NA</VCBO_RESULT>
<PRODUCT_BRO_DES>116E - OTC Equity Accumulators</PRODUCT_BRO_DES>
<AI_STATUS>NA</AI_STATUS>
<AI_RESULT>Alert</AI_RESULT>
<AI_BLOCKING>NA</AI_BLOCKING>
<PI_ASSET>NA</PI_ASSET>
<PID_STATUS>NA</PID_STATUS>
<PI_RESULT>NA</PI_RESULT>
<PI_CHECK>OK</PI_CHECK>
<PI_BLOCKING>NA</PI_BLOCKING>
<PI_EXCEPTION_MESSAGE/>
<AI_CHECK>NOK</AI_CHECK>
<AI_EXCEPTION_MESSAGE/>
<FIRST_TIME_BUYER>Yes</FIRST_TIME_BUYER>
<PB_STATUS xsi:nil="true"/>
<PB_RESULT>NA</PB_RESULT>
<PB_CHECK>OK</PB_CHECK>
<PB_BLOCKING>NA</PB_BLOCKING>
<PB_EXCEPTION_MESSAGE/>
<FIRST_TIME_BUYER_CHECK>NOK</FIRST_TIME_BUYER_CHECK>
<PI_REGULATORY/>
<EXCEPTION_MSG/>
<REQUEST_ID>COPSHK-REQ-01</REQUEST_ID>
<EXCEPTION_IND>N</EXCEPTION_IND>
</element>
</RESPONSES>
</output>
<vr:trace>
<ruleModel>UBS_Investment_Suitability_Models</ruleModel>
<rulePath>/UBS_Investment_Suitability_Models/executeInvestmentSuitability_OnPersonPlacingOrder</rulePath>
<version>2.0.0</version>
<requestId>a42ee1e4-1cf4-11e2-8254-f3be1042c87c</requestId>
</vr:trace>
</vrresponse:VRResponse>
</soapenv:Body>
</soapenv:Envelope>
With the help of "woodstox-core-asl-4.1.2.jar", we can skip some tag, so JAXB can understand.
为什么?因为CXF生成java的代码是依赖WSDL. 但是WSDL没有包括soap信封的定义