Webservice-Client: use textual xml instead of object hierarchy

I am programming a simple proxy server in java:

  • Reading XML File
  • Submit a web service request
  • Read web service response
  • Write an answer to a file

My first attempt was to use JAXB to read an XML file and create Java objects. Then I submit objects using JAX-WS (IBM WebSphere). I get the response as "ResponseObject", which is then generated in xml code. I am writing xml code to a file.

This setting works well. But...

When sending java objects to the WebService, xml is generated, and the response creates java objects again. I really don't need these request and response objects. Is there any way to directly call xml plaintext WebService? And read the answer as plain xml text instead of these response objects?

(Assume xml files are always valid.)

thank

+3
source share
1 answer

You can use SAAJ (SOAP with Attachment API for Java), which works at a lower level than JAX-WS. I expect it to use less system resources than JAX-WS.

. ( users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm)

SOAP-, , .

1-13. SOAP : prepare.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>

, .

1-14. SOAP- , -, 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());
      }
    }
  }
+3

Source: https://habr.com/ru/post/1730984/


All Articles