달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
원문 : http://edocs.bea.com/wls/docs61/webServices/advanced.html


Invoking Web Services Without Using the WSDL File


This Appendix shows an example of a dynamic client application that does not use the WSDL file when it invokes a WebLogic Web Service. In particular, the example invokes a message-style Web service and sends data to WebLogic Server.

Dynamic client applications that do not use the WSDL of the Web service are dynamic in every way, because they can invoke a Web service without knowing either the interface of the Web service, or the JavaBean interface of return values and parameters, or even the number and signatures of the methods that make up the Web service.

The example uses the URL http://www.myHost.com:7001/msg/sendMsg to invoke the Web Service. Because the example shows a dynamic client application that does not use the WSDL of the Web service, the preceding URL is for the Web service itself, rather than the URL for the WSDL of the Web service.

The procedure after the example discusses relevant sections of the example as part of the basic steps you follow to create this client.

import java.util.Properties;
import java.net.URL;
import javax.naming.Context;
import javax.naming.InitialContext;
import weblogic.soap.WebServiceProxy;
import weblogic.soap.SoapMethod;
import weblogic.soap.SoapType;
import weblogic.soap.codec.CodecFactory;
import weblogic.soap.codec.SoapEncodingCodec;
import weblogic.soap.codec.LiteralCodec;
public class ProducerClient{
  public static void main( String[] arg ) throws Exception{
    CodecFactory factory = CodecFactory.newInstance();
    factory.register( new SoapEncodingCodec() );
    factory.register( new LiteralCodec() );
    WebServiceProxy proxy = WebServiceProxy.createService( 
       new URL( "http://www.myHost.com:7001/msg/sendMsg" ) );
    proxy.setCodecFactory( factory );
    proxy.setVerbose( true );
    SoapType param = new SoapType( "message", String.class );
    proxy.addMethod( "send", null, new SoapType[]{ param } ); 
    SoapMethod method = proxy.getMethod( "send" );
    String toSend = arg.length == 0 ? "No arg to send" : arg[0];
    Object result = method.invoke( new Object[]{ toSend } );
  } 
}

Follow these steps to create a dynamic Java client that does not use WSDL to invoke a message-style WebLogic Web Service that sends data to WebLogic Server:

  1. Get the Java client JAR file from the WebLogic Server hosting the WebLogic Web Service.

    For detailed information on this step, refer to Downloading the Java Client JAR File from the Web Services Home Page.

  2. Add the Java client JAR file to your CLASSPATH on your client computer.
  3. Create the client Java program. The following steps describe the Web services-specific Java code:

    1. In the main method of your client application, create a factory of encoding styles and register the two that are supported by WebLogic Server (the SOAP encoding style and Apache's Literal XML encoding style):
      CodecFactory factory = CodecFactory.newInstance();
      factory.register( new SoapEncodingCodec() );
      factory.register( new LiteralCodec() );
      
    2. Add the following Java code to create the connection to the Web service and set the encoding style factory:
      WebServiceProxy proxy = WebServiceProxy.createService( 
             new URL( "http://www.myHost.com:7001/msg/sendMsg" ) );
      proxy.setCodecFactory( factory );
      proxy.setVerbose( true );
      
    3. Add the following Java code to dynamically get the send method of the Web service:
       SoapType param = new SoapType( "message", String.class );
       proxy.addMethod( "send", null, new SoapType[]{ param } ); 
       SoapMethod method = proxy.getMethod( "send" );
      
    4. Invoke the send method and send data to the Web service. In the example, the client application simply takes its first argument and sends it as a String; if the user does not specify an argument specified, then the client application sends the string No arg to send:
      String toSend = arg.length == 0 ? "No arg to send" : arg[0];
      Object result = method.invoke( new Object[]{ toSend } );
      
  4. Compile and run the client Java program as usual.

The following more complex example shows how to use a send method that accepts a org.w3c.dom.Document, org.w3c.dom.DocumentFragment, or org.w3c.dom.Element data type as its parameter. The example shows how to set literal encoding on this flavor of the send method.

import java.util.Properties;
import java.net.URL;
import java.io.File;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import weblogic.apache.xml.serialize.OutputFormat;
import weblogic.apache.xml.serialize.XMLSerializer;
import weblogic.apache.xerces.dom.DocumentImpl;
import weblogic.soap.WebServiceProxy;
import weblogic.soap.SoapMethod;
import weblogic.soap.SoapType;
import weblogic.soap.codec.CodecFactory;
import weblogic.soap.codec.SoapEncodingCodec;
import weblogic.soap.codec.LiteralCodec;
public class ProducerClient{
  public static void main(String[] args) throws Exception{
    String url = "http://localhost:7001";
    // Parse the arguments list
    if (args.length != 2) {
      System.out.println("Usage: java examples.webservices.message.ProducerClient 
http://hostname:port \"message\"");
      return;
    } else if (args.length == 2) {
      url = args[0];
    }
    CodecFactory factory = CodecFactory.newInstance();
    factory.register(new SoapEncodingCodec());
    factory.register(new LiteralCodec());
    URL newURL = new URL(url + "/msg/sendMsg");
    WebServiceProxy proxy = WebServiceProxy.createService(newURL);
    proxy.setCodecFactory(factory);
    proxy.setVerbose(true);
    SoapType param = new SoapType( "message", Document.class );
    proxy.addMethod( "send", null, new SoapType[]{ param } );
    SoapMethod method = proxy.getMethod("send");
    // Print out proxy to make sure method signature looks good
    System.out.println("Proxy:"+proxy);
    DocumentBuilderFactory dbf =
                     DocumentBuilderFactory.newInstance();
    //Obtain an instance of a DocumentBuilder from the factory.
    DocumentBuilder db = dbf.newDocumentBuilder();
    //Parse the document.
    Document w3cDoc = db.parse(new File("/test/fdr_nodtd.xml"));
    //Class parserClass = Class.forName("org.jdom.adapters.XercesDOMAdapter");
    //DOMAdapter da = (DOMAdapter)parserClass.newInstance();
    //Document w3cDoc = da.getDocument(new File("/test/fdr_nodtd.xml"),false);
    // Print out XML just to make sure the document was read successfully
    OutputFormat of = new OutputFormat();
    of.setEncoding("UTF-8");
    of.setLineWidth(40);
    of.setIndent(4);
    XMLSerializer xs = new XMLSerializer(System.out,of);
    xs.serialize(w3cDoc);
    System.out.println("Before Invoke");
    Object result = method.invoke( new Object[]{w3cDoc} );
    System.out.println("Done");
  }
}
Posted by tornado
|