1. Example 1 (mainly see [2])
1.1. System functions:
Develop a calculator service CalculateService, which includes operations such as plus, subtraction, multiply, and dividing.
1.2. Preparation before development:
Install Eclipse-jee;
Download the latest version of Axis2, the URL is http://axis.apache.org/axis2/java/core/download.cgi, select the zip package of Standard Binary Distribution, and unzip the directory name axis2-1.4.1. The file structure in the directory is as follows:
1.3. Pre-development configuration:
In the menu bar of Eclipse, Window --> Preferences --> Web Service --> Axis2 Perferences, select the location of the Axis2 decompression package in the Axis2 runtime location. After setting it, click "OK". (As shown in the picture)
1.4. Develop Web Service:
(1) Create a new Java Project and name it "WebServiceTest1"
(2) Create a new class and name it "CalculateService". The complete code is as follows:
package edu.sjtu.webservice; /** * Calculator operation* @author rongxinhua */ public class CalculateService { //Addition public float plus(float x, float y) { return x + y; } //Subtraction public float minus(float x, float y) { return x - y; } //Multiple public float multiply(float x, float y) { return x * y; } //Dividation public float divide(float x, float y) { if(y!=0) { return x / y; } else return -1; } }(3) On the "WebServiceTest1" project new --> other, find the "Web Service" under "Web Services";
(4) Next step (next), in the Web Services object box that appears, click "Browse" in the Service implementation, enter the Browse Classes object box, and find the CalculateService class we just wrote. (Figure below). Click "ok", and you will return to the Web Service dialog box.
(5) In the Web Service dialog box, adjust the slider in the Web Service type to the position of "start service" and adjust the slider in the Client type to the position of "Test client".
(6) There is a "Configuration" on the right side of the Web Service type slider diagram. Click the option below it to enter the Service Deployment Configuration object box, and select the corresponding Server (I use Tomcat6.0 here) and Web Service runtime (select Apache Axis2), as shown in the figure below:
(7) After clicking OK, it will return to the Web Service dialog box. Similarly, there is also "Configuration" on the right side of the slider in the Client type, and the corresponding setting must be performed. The steps are the same as above. After completion, Next --> next is ready. Entering Axis2 Web Service Java Bean Configuration, we select Generate a default services.xml, as shown in the figure below:
(8) When you arrive at the Server startup dialog box, there is a button "start server" (as shown below). Click it to start the Tomcat server.
(9) After waiting for the startup, click "next --> next", everything will be done by default, and finally, click Finish. Finally, the following interface appears: (Web Service Explorer), where we can test our Web services. (If you open it with a browser, use the following address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3). As shown in the figure below:
Note: Open Web Service Explorer in the browser (sometimes the webservice explorer is closed in eclipse, which can be opened in this way)
First login address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp. Then select the Web Service Explorer tab in the upper right corner of the web page. Then enter the WSDL address: http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl. This wsdl address is the wsdl we just released the service. Click go, as shown in the following figure:
Then you can see the following interface:
(10) The test is relatively simple. For example, we select an "plus" Operation (must be CalculateServiceSoap11Binding). The following figure appears. Enter 1 in the input box of x, enter 2 in the input box of y, and click "go", and the result 3.0 will be displayed in the status bar. The tests of other methods are similar. The results are shown in the figure above.
1.5. CalculateService client calls the program
We have defined the methods of addition, subtraction, multiplication and division and published these methods as services. Now all we need to do is to call these services. The client call program as shown in the following code: CalculateServiceTest.java
package edu.sjtu.webservice.test; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class CalculateServiceTest { /** * @param args * @throws AxisFault */ public static void main(String[] args) throws AxisFault { // TODO Auto-generated method stub // Call WebService using RPC method RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // Specify the URL to call WebService EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/WebServiceTest1/services/CalculateService"); options.setTo(targetEPR); // Specify the method and namespace of the WSDL file in the computer to be called: edu.sjtu.webservice. QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//AddQName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//SubtractionQName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//Multiply QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//Divide// Specify the parameter values of the plus method are two, namely the adder and the added number Object[] opAddEntryArgs = new Object[] { 1,2 }; // Specify the data type of the return value of the plus method Class[] classes = new Class[] { float.class }; // Call the plus method and output the return value of the method System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]); } }Running results:
3.0
-1.0
2.0
0.5
2. Example
2.HelloService
(1) First define the service method, the code is as follows:
package edu.sjtu.webservice; public class HelloService { public String saysHelloNew() { return "hello"; } public String saysHelloToPersonNew(String name) { if (name == null) { name = "nobody"; } return "hello," + name; } public void updateData(String data) { System.out.println(data + "Updated."); } } (2) Refer to Example 1 to publish this method as a service.
(3) Write client code to call WebService (main reference [5])
The biggest difference between this example and other examples is here. Other examples generally need to generate a client stub based on the service WSDL just now, and then call the service through the stub. This method seems relatively single. The client must need a stub stub to access the service, which is very unfair. The client in this example does not use the stub method, but implements a general calling method, and does not require any client stub to access the service. You only need to specify the web servce address, operation name, parameter and function return type. The code looks like this:
HelloServiceTest2.javapackage edu.sjtu.webservice.test; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class HelloServiceTest2 { private RPCServiceClient serviceClient; private Options options; private EndpointReference targetEPR; public HelloServiceTest2(String endpoint) throws AxisFault { serviceClient = new RPCServiceClient(); options = serviceClient.getOptions(); targetEPR = new EndpointReference(endpoint); options.setTo(targetEPR); } public Object[] invokeOp(String targetNamespace, String opName, Object[] opArgs, Class<?>[] opReturnType) throws AxisFault, ClassNotFoundException { // Set the name of the operation QName opQName = new QName(targetNamespace, opName); // Set the return value// Class<?>[] opReturn = new Class[] { opReturnType }; // The parameters that the operation needs to be passed in are already given in the parameters. Here we directly call return serviceClient.invokeBlocking(opQName, opArgs, opReturnType); } /** * @param args * @throws AxisFault * @throws ClassNotFoundException */ public static void main(String[] args) throws AxisFault, ClassNotFoundException { // TODO Auto-generated method stub final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService"; final String targetNamespace = "http://webservice.sjtu.edu"; HelloServiceTest2 client = new HelloServiceTest2(endPointReference); String opName = "sayHelloToPersonNew"; Object[] opArgs = new Object[] { "My Friends" }; Class<?>[] opReturnType = new Class[] { String[].class }; Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType); System.out.println(((String[]) response[0])[0]); } }Run the program and click Run As->Java application. You can see that the output of the console port is: Hello, My Friends, indicating that the client call is successful. The biggest difference and advantage of this example is reflected in the client's calling method, or in other words, the way to initiate service calls. Although there is a little more code than the client's stub stub stub, this method is unified and does not require production of stub stub code, which solves the problem that the client has many classes. If the reader further encapsulates these codes, I think the call method is simple, just pass relevant parameters, which better illustrates the advantages of service calls. Moreover, this method is simpler and clearer, and you can tell the specific meaning at a glance. There is no need to make some mechanisms of stub class.
(4) Rewrite the code of the client calling service
(3) The client application code mentioned in the following is a little complicated to write. The above client calling the service program is rewritten, which is much simpler. The code is as follows:
HelloServiceTest.javaimport javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class HelloServiceTest { public static void main(String args[]) throws AxisFault { // Call WebService RPCServiceClient using RPC serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // Specify the URL to call WebService EndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService"); options.setTo(targetEPR); // Specify the sayHelloToPerson method to be called and the namespace of the WSDL file QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew"); // Specify the parameter value of the sayHelloToPerson method Object[] opAddEntryArgs = new Object[] { "xuwei" }; // Specify the Class object of the data type that the sayHelloToPerson method returns the value Class[] classes = new Class[] { String.class }; // Call the sayHelloToPerson method and output the return value of the method System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]); } }Through the above content, I will introduce a simple development example of Eclipse+Webservice, and I hope it will be helpful to everyone!