gpt4 book ai didi

java - SOAP 请求中的数据处理程序

转载 作者:行者123 更新时间:2023-11-30 05:51:48 24 4
gpt4 key购买 nike

Web 服务要求我设置一个 DataHandler 类型,该类型应该是 xml 附件。

DataSource dataSource = new FileDataSource(tempFile.getAbsolutePath()); 
DataHandler dataHandler = new DataHandler(dataSource);
request.setDataHandler(dataHandler);

问题是从 Axis2 生成的 SOAPMessage 的值为 base64

<dataHandler>big string64 string representing my content</dataHandler>

它应该在哪里

<dataHandler><inc:Include href="cid:attachmentid" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></dataHandler>

Content-Type: text/xml; charset=us-ascii; name=Sample.xml
Content-Transfer-Encoding: 7bit
Content-ID: <attachmentid>
Content-Disposition: attachment; name="Sample.xml"; filename="Sample.xml"

... the xml content....

WSDL

  <xsd:element name="dataHandler" type="xsd:base64Binary" maxOccurs="1" minOccurs="1" xmime:expectedContentTypes="application/octet-stream"/>

我该如何解决这个问题?

最佳答案

我的 xmlobject 中有一个值,它是一个 DataHandler,这是我的解决方案,用于将数据处理程序作为内部包含 xml 的对象放入该值中:

带有根元素的 XmlObject:

RootXmlObject xml = new RootXmlObject();

然后设置编码器以转换为字符串:

JAXBContext context = JAXBContext.newInstance(RootXmlObject.class);
Marshaller mar= context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
mar.marshal(xml, sw);
String xmlString = sw.toString();

创建自定义数据处理程序:

DataHandler.setDataContentHandlerFactory(new YourDatahandler());
private class YourDatahandler implements DataContentHandlerFactory {

@Override
public DataContentHandler createDataContentHandler(String mimeType) {

return new XmlDataContentHandler();
}
}

public static class XmlDataContentHandler implements DataContentHandler {


@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {DataFlavor.stringFlavor};
}
@Override
public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
return new String("Whateverstring");
}

@Override
public Object getContent(DataSource dataSource) throws IOException {
return new String("Whateverstring");

哪个“writeTo”方法的工作原理如下:

    @Override
public void writeTo(Object o, String s, OutputStream outputStream) throws IOException {
byte[] stringByte = (byte[]) ((String) o).getBytes("UTF-8");
outputStream.write(stringByte);
}

最后将 xmlString 插入数据处理程序:

DataHandler testHandler = new DataHandler(xmlString, "text/xml");

这是我的项目的链接:https://github.com/habelo/domibusdemo

关于java - SOAP 请求中的数据处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53764692/

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