gpt4 book ai didi

java - Web服务客户端: Use plain-text xml instead of object hierarchy

转载 作者:数据小太阳 更新时间:2023-10-29 02:28:14 26 4
gpt4 key购买 nike

我正在用 java 编写一个简单的代理:

  1. 读取 XML 文件
  2. 向网络服务发送请求
  3. 读取网络服务响应
  4. 将响应写入文件

我的第一次尝试是使用 JAXB 读取 xml 文件并生成 Java 对象。然后我使用 JAX-WS (IBM WebSphere) 发送对象。我收到作为“ResponseObject”的响应,然后将其生成为 xml 代码。我将 XML 代码写入文件。

此设置效果很好。但是……

将 java 对象发送到 WebService 时,会生成 xml,响应会再次创建 java 对象。我真的不需要那些请求和响应对象。有没有一种方法可以直接使用纯文本 xml 调用 WebService? 并以纯文本 xml 的形式读取响应,而不是那些响应对象?

(假设 xml 文件始终有效。)

谢谢

最佳答案

可以使用 SAAJ (SOAP with Attachments API for Java)运行在比 JAX-WS 更低的级别。我希望它比 JAX-WS 使用更少的系统资源。

请参阅以下示例(从 users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm 复制)

No more dynamic construction of the SOAP message this time, let's use a simple text editor and type the soap message we want to send.

Example 1-13. The formated SOAP message in a text file: prepared.msg

<SOAP-ENV:Envelope 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header/><SOAP-ENV:Body>
<ans1:readLS xmlns:ans1="http://phonedirlux.homeip.net/types">
<String_1 xsi:type="xsd:string">your message or e-mail</String_1>
</ans1:readLS>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Now the code will be shorter and you can easily use it for testing purpose.

Example 1-14. Post a SOAP message in a text file, to a web service using SAAJ

  import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import java.io.FileInputStream;
import javax.xml.transform.stream.StreamSource;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;

import javax.xml.transform.stream.StreamResult;

public class Client {

public static void main(String[] args) {

try {
// Create the connection
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();

// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// Object for message parts
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(
new FileInputStream("path/prepared.msg"));
sp.setContent(prepMsg);

// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");
msg.writeTo(System.out);
System.out.println();

// Send
String urlval = "http://www.pascalbotte.be/rcx-ws/rcx";
SOAPMessage rp = conn.call(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
Source sc = rp.getSOAPPart().getContent();

// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();

// Close connection
conn.close();

}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

关于java - Web服务客户端: Use plain-text xml instead of object hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2182747/

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