编程语言
首页 > 编程语言> > java-Webservice-Client:使用纯文本xml而不是对象层次结构

java-Webservice-Client:使用纯文本xml而不是对象层次结构

作者:互联网

我正在用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());
      }
    }
  }

标签:jax-ws,web-services,xml,java
来源: https://codeday.me/bug/20191106/2001505.html