Java axis calls the WebService server, the client

Source: Internet
Author: User
Tags wsdl

Service side:

1, Calculator

Package ws;


public class Calculator {

public int Add (int a, int b) {

return (A + B);

}


public int subtract (int a, int b) {

Return (A-B);

}


public int multiply (int a, int b) {

Return (A * b);

}


public int divide (int a, int b) {

Return (A/b);

}


}

Generate WSDL See attachments


Client:

2. Introduction of Axis's jar package

3, Test01

Package Test;


Import Javax.xml.namespace.QName;


Import org.apache.axis2.addressing.EndpointReference;

Import org.apache.axis2.client.Options;

Import org.apache.axis2.rpc.client.RPCServiceClient;


public class Test01 {


public static void Main (string[] args) {

String url = "Http://localhost:8080/WebserviceTest/services/Calculator";

String result = null;

try {

Call WebService using RPC mode

Rpcserviceclient serviceclient = new Rpcserviceclient ();

Options options = Serviceclient.getoptions ();

Call WebService using RPC mode

EndpointReference Targetepr = new EndpointReference (URL);

Options.setto (TARGETEPR);

When you create a QName object, the first parameter of the constructor method of the QName class represents the namespace name of the WSDL file, which is the targetnamespace property value of the <wsdl:definitions> element

Specifies the namespace of the Add method and WSDL file to invoke .....

QName add = new QName ("http://csh.bzu.com", "add");

Specify the parameter value of the Add method, if there are multiple, continue to add to the back, without specifying the name of the parameter

object[] Opaddentryargs = new object[] {2, 3};

Return parameter type

The Invokeblocking method has three parameters, where the type of the first parameter is the QName object that represents the method name to invoke;

The second parameter represents the parameter value of the WebService method to invoke, and the parameter type is object[];

The third parameter represents the class object of the return value type of the WebService method, and the parameter type is class[].

When a method has no arguments, the second parameter value of the Invokeblocking method cannot be null, and the new object[]{} is used

If the called WebService method does not return a value, the Invokerobust method of the Rpcserviceclient class should be used.

The method has only two parameters, and they have the same meaning as the first two parameters of the Invokeblocking method

Class object specifying the data type of the GetGreeting method return value .....

Class[] classes = new class[] {string.class};

Call the Add method and output the return value of the method ....

result = (String) serviceclient.invokeblocking (Add, opaddentryargs,classes) [0];

System.out.println ("The Add result is:" + result);

Here's the code that calls the Subtract method, similar to the code that calls the Getworld method

QName subtract = new QName ("http://csh.bzu.com", "subtract");

object[] opAddEntryArgs1 = new object[] {2, 3};


class[] classes1 = new class[] {string.class};

result = (String) serviceclient.invokeblocking (subtract, opaddentryargs1,classes1) [0];

System.out.println ("The subtract result is:" + result);

Here's the code that calls the multiply method, similar to the code that calls the Getworld method

QName multiply = new QName ("http://csh.bzu.com", "multiply");

object[] OpAddEntryArgs2 = new object[] {2, 3};

class[] Classes2 = new class[] {string.class};

result = (String) serviceclient.invokeblocking (multiply, opaddentryargs2,classes2) [0];

System.out.println ("The multiply result is:" + result);

Here's the code that calls the Divide method, similar to the code that calls the Getworld method

QName divide = new QName ("http://csh.bzu.com", "divide");

object[] OpAddEntryArgs3 = new object[] {2, 3};

class[] Classes3 = new class[] {string.class};

result = (String) serviceclient.invokeblocking (Divide, Opaddentryargs3,classes3) [0];

System.out.println ("The Divide result is:" + result);

} catch (Exception e) {

E.printstacktrace ();

}

}

}





This article is from the "Database Siege Lion" blog, please make sure to keep this source http://liu16.blog.51cto.com/6494747/1729324

Java axis calls the WebService server, the client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.