Java calls methods of the WebService interface

  • 2020-04-01 03:40:31
  • OfStack

This article illustrates how Java calls the WebService interface. Share with you for your reference. The details are as follows:

Here is the reference method Add, the code is as follows:

public static void addTest() {
        try ...{
            Integer i = 1;
            Integer j = 2;
           
            //WebService URL
            String service_url = "http://localhost:4079/ws/Service.asmx";
           
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(service_url));
           
            //Set the method to be called
            call.setOperationName(new QName("//www.jb51.net/T", "Add"));
           
            //The parameters required for this method are
            call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT,
                    javax.xml.rpc.ParameterMode.IN);
           
            //Method returns a value of type
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
           
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("//www.jb51.net/Add");
           
            //Call the method
            Integer res = (Integer)call.invoke(
                    new Object[]...{
                        i, j
                    }
            );
           
            System.out.println( "Result: " + res.toString());
           
        } catch (Exception e) ...{
            System.err.println(e);
        }
}

Run, and the Result returns: Result: 3

I hope this article has been helpful to your Java programming.


Related articles: