java-Webservice-Client:使用纯文本xml而不是对象层次结构
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-Webservice-Client:使用纯文本xml而不是对象层次结构,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3131字,纯文字阅读大概需要5分钟。
内容图文
![java-Webservice-Client:使用纯文本xml而不是对象层次结构](/upload/InfoBanner/zyjiaocheng/671/08970a495dbb4d079f760dd6f0d6dfa5.jpg)
我正在用Java编写一个简单的代理:
>读取XML文件
>将请求发送到Web服务
>阅读Web服务响应
>将响应写入文件
我的第一次尝试是使用JAXB读取xml文件并生成Java对象.
然后,我使用JAX-WS(IBM WebSphere)发送对象.
我收到的响应是“ ResponseObject”,然后将其生成为xml代码.我将XML代码写入文件.
此设置效果很好.但…
将Java对象发送到WebService时,将生成xml,并且响应还会创建Java对象.我真的不需要那些请求和响应对象.
有没有一种方法可以使用纯文本xml直接调用WebService?并以纯文本xml而不是那些响应对象的形式读取响应?
(假设xml文件始终有效.)
谢谢
解决方法:
可以使用SAAJ(带有Java附件API的SOAP)运行,其运行级别低于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-Webservice-Client:使用纯文本xml而不是对象层次结构全部内容,希望文章能够帮你解决java-Webservice-Client:使用纯文本xml而不是对象层次结构所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。