Spring boot 如何防止soapConnection.call剥离CDATA-Spring启动应用程序
我使用以下代码调用soapweb服务(来自springboot应用程序)。web服务将在其中一个元素(结果)中返回一个包含XML(在CDATA中)的响应。我的需求是解组soap响应并将其转换为java对象。我没有修改soap服务的权限。根据soap请求,结果元素将具有字符串值或XML(在CDATA中)。当结果元素具有字符串值时,我的解组程序可以完美地工作,但如果结果元素具有不带CDATA的XML,则会失败Spring boot 如何防止soapConnection.call剥离CDATA-Spring启动应用程序,spring-boot,soap,soapui,unmarshalling,cdata,Spring Boot,Soap,Soapui,Unmarshalling,Cdata,我使用以下代码调用soapweb服务(来自springboot应用程序)。web服务将在其中一个元素(结果)中返回一个包含XML(在CDATA中)的响应。我的需求是解组soap响应并将其转换为java对象。我没有修改soap服务的权限。根据soap请求,结果元素将具有字符串值或XML(在CDATA中)。当结果元素具有字符串值时,我的解组程序可以完美地工作,但如果结果元素具有不带CDATA的XML,则会失败 SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(<SOAPMessage request>, <soapEndpointUrl>);
应用程序的输出
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://www.someurl/su">
<Result><?xml version="1.0"
encoding="utf-8"?><MyName><System
System="ABC" ValidLogin="False" Reason="User ID not found in
system." /></MyName></Result>
</Response>
</soap:Body>
</soap:Envelope>
如果我手动将CDATA添加到SOAP响应中,则解组将起作用。有没有办法通过CDATA获得soap响应。非常感谢你的帮助
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://www.someurl/su">
<Result><?xml version="1.0"
encoding="utf-8"?><MyName><System
System="ABC" ValidLogin="False" Reason="User ID not found in
system." /></MyName></Result>
</Response>
</soap:Body>
</soap:Envelope>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://www.someurl/su">
<Result><![CDATA[<?xml version="1.0" encoding="utf-8"?><MyName><System System="ABC" ValidLogin="False" Reason="User ID not found in system."/></MyName>]]></Result>
</Response>
</soap:Body>
</soap:Envelope>
String soapBodyStr = "";
SOAPBody soapBody = soapResponse.getSOAPBody();
Document doc = soapBody.extractContentAsDocument();
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "script");
transformer.transform(domSource, result);
writer.flush();
soapBodyStr = writer.toString();
soapBodyStr = soapBodyStr.replaceAll("<", "<");
soapBodyStr = soapBodyStr.replaceAll(">", ">");
soapConnection.close();
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Response xmlns="http://www.someurl/su">
<Result><?xml version="1.0"
encoding="utf-8"?><MyName><System
System="ABC" ValidLogin="False" Reason="User ID not found in
system." /></MyName></Result>
</Response>
StringReader stringReader = new StringReader(soapBodyStr);
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response response = (Response) unmarshaller.unmarshal(stringReader); //getting error here
String soapResponse = Response.getResult();
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 27; The processing instruction target matching "[xX][mM][lL]" is not allowed.]