gpt4 book ai didi

java - 如何修复文件未将自身附加到 Java 中的 SAAJ SOAP 消息的问题?

转载 作者:行者123 更新时间:2023-12-02 04:33:22 26 4
gpt4 key购买 nike

我正在尝试为 SOAP Web 服务构建客户端。我正在将 xml 文件中的请求读取到 String 并将其转换为 SOAPMessage 类。 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 类:

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() + ">");

关于java - 如何修复文件未将自身附加到 Java 中的 SAAJ SOAP 消息的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com