如何修复文件未将自身附加到 Java 中的 SAAJ SOAP 消息?
How to fix file not attaching itself to a SAAJ SOAP message in Java?
我正在尝试为 SOAP Web 服务构建客户端。我正在将 xml 文件中的请求读取到字符串并将其转换为 SOAPMessage class。 SOAP 消息的主体有两个元素,其中之一应该是一个附加文件(元素具有标记 DataFile)。因此,我从一个文件创建一个 AttachmentPart 并将其添加到 SOAPMessage。但是,接收应用程序说消息没有附加文件(消息的 DataHandler==null)。谁能告诉我为什么文件没有随请求一起正确发送?
这是来自 xml 文件的 SOAP 请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xro="http://x-road.eu/xsd/xroad.xsd" xmlns:iden="http://x-road.eu/xsd/identifiers" xmlns:stat="http://stat-v6.x-road.eu">
<soapenv:Header>
<xro:protocolVersion>4.0</xro:protocolVersion>
<xro:issue></xro:issue>
<xro:userId>EE700031011</xro:userId>
<xro:id>123</xro:id>
<xro:service iden:objectType="SERVICE">
<iden:xRoadInstance>ee-test</iden:xRoadInstance>
<iden:memberClass>GOV</iden:memberClass>
<iden:memberCode>70000332</iden:memberCode>
<!--Optional:-->
<iden:subsystemCode>estat</iden:subsystemCode>
<iden:serviceCode>SubmitData</iden:serviceCode>
<!--Optional:-->
<iden:serviceVersion>v1</iden:serviceVersion>
</xro:service>
<xro:client iden:objectType="SUBSYSTEM">
<iden:xRoadInstance>ee-test</iden:xRoadInstance>
<iden:memberClass>GOV</iden:memberClass>
<iden:memberCode>70000332</iden:memberCode>
<!--Optional:-->
<iden:subsystemCode>sa-client</iden:subsystemCode>
</xro:client>
</soapenv:Header>
<soapenv:Body>
<stat:SubmitDataRequest>
<DataFile>cid:Report.xml</DataFile>
<XSDValidationOnly>1</XSDValidationOnly>
</stat:SubmitDataRequest>
</soapenv:Body>
</soapenv:Envelope>
Report.xml 是我要附加到 SOAP 消息的文件。
这是我的 SoapClient class:
import com.sun.istack.internal.Nullable;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.sax.SAXSource;
import java.io.*;
import java.net.URL;
public class SoapClient {
// soapMessage is the request read from an xml file
SOAPMessage callSoapWebServiceFromString(String soapEndpointUrl, String soapMessage, @Nullable File attachment) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
message.getSOAPPart().setContent(new SAXSource(new InputSource(new StringReader(soapMessage))));
if (attachment != null) {
DataHandler handler = new DataHandler(new FileDataSource(attachment));
AttachmentPart attachPart = message.createAttachmentPart(handler);
message.addAttachmentPart(attachPart);
message.saveChanges();
// String stringToWrite = IOUtils.toString(handler.getInputStream(), "utf-8");
// System.out.println("stringToWrite: " + stringToWrite);
}
URL endpoint = new URL(soapEndpointUrl);
SOAPMessage response = soapConnection.call(message, endpoint);
// System.out.println("Response SOAP Message:");
// response.writeTo(System.out);
soapConnection.close();
return response;
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
return null;
}
}
找到答案:attachPart.setContentId("<" + attachment.getName() + ">");
我正在尝试为 SOAP Web 服务构建客户端。我正在将 xml 文件中的请求读取到字符串并将其转换为 SOAPMessage class。 SOAP 消息的主体有两个元素,其中之一应该是一个附加文件(元素具有标记 DataFile)。因此,我从一个文件创建一个 AttachmentPart 并将其添加到 SOAPMessage。但是,接收应用程序说消息没有附加文件(消息的 DataHandler==null)。谁能告诉我为什么文件没有随请求一起正确发送?
这是来自 xml 文件的 SOAP 请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xro="http://x-road.eu/xsd/xroad.xsd" xmlns:iden="http://x-road.eu/xsd/identifiers" xmlns:stat="http://stat-v6.x-road.eu">
<soapenv:Header>
<xro:protocolVersion>4.0</xro:protocolVersion>
<xro:issue></xro:issue>
<xro:userId>EE700031011</xro:userId>
<xro:id>123</xro:id>
<xro:service iden:objectType="SERVICE">
<iden:xRoadInstance>ee-test</iden:xRoadInstance>
<iden:memberClass>GOV</iden:memberClass>
<iden:memberCode>70000332</iden:memberCode>
<!--Optional:-->
<iden:subsystemCode>estat</iden:subsystemCode>
<iden:serviceCode>SubmitData</iden:serviceCode>
<!--Optional:-->
<iden:serviceVersion>v1</iden:serviceVersion>
</xro:service>
<xro:client iden:objectType="SUBSYSTEM">
<iden:xRoadInstance>ee-test</iden:xRoadInstance>
<iden:memberClass>GOV</iden:memberClass>
<iden:memberCode>70000332</iden:memberCode>
<!--Optional:-->
<iden:subsystemCode>sa-client</iden:subsystemCode>
</xro:client>
</soapenv:Header>
<soapenv:Body>
<stat:SubmitDataRequest>
<DataFile>cid:Report.xml</DataFile>
<XSDValidationOnly>1</XSDValidationOnly>
</stat:SubmitDataRequest>
</soapenv:Body>
</soapenv:Envelope>
Report.xml 是我要附加到 SOAP 消息的文件。
这是我的 SoapClient class:
import com.sun.istack.internal.Nullable;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.transform.sax.SAXSource;
import java.io.*;
import java.net.URL;
public class SoapClient {
// soapMessage is the request read from an xml file
SOAPMessage callSoapWebServiceFromString(String soapEndpointUrl, String soapMessage, @Nullable File attachment) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
message.getSOAPPart().setContent(new SAXSource(new InputSource(new StringReader(soapMessage))));
if (attachment != null) {
DataHandler handler = new DataHandler(new FileDataSource(attachment));
AttachmentPart attachPart = message.createAttachmentPart(handler);
message.addAttachmentPart(attachPart);
message.saveChanges();
// String stringToWrite = IOUtils.toString(handler.getInputStream(), "utf-8");
// System.out.println("stringToWrite: " + stringToWrite);
}
URL endpoint = new URL(soapEndpointUrl);
SOAPMessage response = soapConnection.call(message, endpoint);
// System.out.println("Response SOAP Message:");
// response.writeTo(System.out);
soapConnection.close();
return response;
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
return null;
}
}
找到答案:attachPart.setContentId("<" + attachment.getName() + ">");