Eclipse+webservice Simple Development Example _java

Source: Internet
Author: User
Tags stub tomcat server wsdl

1. Instance 1 (see mainly [2])

1.1. System function:

Develop a calculator service Calculateservice, which includes addition (plus), subtraction (minus), multiplication (multiply), except (divide) operations.

1.2. Pre-development preparation:

Install Eclipse-jee;

Download the latest version of Axis2, URL http://axis.apache.org/axis2/java/core/download.cgi, select standard Binary distribution Zip package, Extract the resulting directory name axis2-1.4.1, the file structure within the directory is as follows:


1.3. Pre-development configuration:

In the Eclipse menu bar, window--> Preferences--> Web Service--> Axis2 perferences, Axis2 Location Select the location of the AXIS2 uncompressed package, set up, click "OK". (as pictured)

1.4. Develop Web Service:

(1) Create a new Java Project named "WebServiceTest1"
(2) Create a new class, named "Calculateservice", complete code 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 
xy; 
} 
Multiplication public 
float multiply (float x, float y) {return 
x * y; 
} 
Division public 
Float divide (float x, float y) { 
if (y!=0) 
{return 
x/y; 
} 
else 
return-1; 
} 

(3) on "WebServiceTest1" project New--> Other, find "Web Services" under the "Web Service";

(4) Next, in the Web Services object box that appears, click "Browse" in the service implementation to enter the Browse Classes object box, Find the Calculateservice class that we just wrote. (pictured below). Click OK to return to the Web Service dialog box.

(5) In the Web Service dialog box, transfer the slider in the Web service type to the location of the start service, and the slider in the client type to the location of Test client.

(6) There is a "Configuration" on the right side of the Web Service type slider graph, click the option below it to enter the service Deployment Configuration Object box, where you can select the appropriate server ( I'm here with Tomcat6.0) and Web Service runtime (choose Apache Axis2), as shown in the following figure:

(7) Click OK, then return to the Web Service dialog box, the same, the Client type in the slider to the right also has "Configuration", but also to carry out the corresponding steps above. When you are done, next--> next. Into the Axis2 Web Service Java Bean Configuration, we chose generate a default services.xml, as shown in the following figure:

(8) To the Server Startup dialog box, there is a key "Start Server" (The following image), click on it, you can start the Tomcat server.

(9) After the completion, click "Next--> Next", all default, and finally, click Finish. Finally, the following interface appears: (Web Service Explorer), where we can test our web services. (Use browser to open words using the following address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3). As shown in the following illustration:

Note: Open Web Service Explorer in the browser (sometimes WebService Explorer is turned off 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 exoplorer tab in the upper-right corner of the page. Then enter the WSDL address: http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl. This WSDL address is the WSDL that we just posted the service on. Click Go, as shown in the following image:


You can then see the following interface:


(10) The test is relatively simple, for example, we choose a "plus" operation (must be calculateservicesoap11binding), the following figure, in the X input box, enter 1, enter 2 in the Y input box, click "Go", The result 3.0 is displayed in the status bar. Tests of other methods are similar. The result is shown in the figure above.

1.5.CalculateService Client Calling Program

We've already defined the subtraction approach and published these methods as services, so all we have to do now is call these services. The client caller program is 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) t Hrows Axisfault {//TODO auto-generated Method Stub//use RPC to invoke WebService rpcserviceclient serviceclient = new Rpcser 
Viceclient (); 
Options options = Serviceclient.getoptions (); Specifies the URL that invokes WebService endpointreference Targetepr = new EndpointReference ("http://localhost:8080/WebServiceTest1/ 
Services/calculateservice "); 
Options.setto (TARGETEPR); 
Specifies the namespace of the method and WSDL file in the computer to invoke: Edu.sjtu.webservice. QName opaddentry = new QName ("http://webservice.sjtu.edu", "plus");/add QName Opaddentryminus = new QName ("http:// Webservice.sjtu.edu "," minus ");/subtraction QName opaddentrymultiply = new QName (" HTTP://WEBSERVICE.SJTU.EDu "," multiply ");/multiplication QName opaddentrydivide = new QName (" http://webservice.sjtu.edu "," divide ");//Division// 
The parameter value of the specified plus method is two, respectively, Addends and addends object[] Opaddentryargs = new object[] {1,2}; 
Specifies the Class object for the data type of the plus method return value 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]); } 
}

Run Result:

3.0
-1.0
2.0
0.5

2. Examples

2.HelloService

(1) First define the service method, as shown in the following code:

Package edu.sjtu.webservice; 
public class HelloService {public 
String sayhellonew () {return 
"Hello"; 
} 
Public String sayhellotopersonnew (string name) { 
if (name = = null) { 
name = ' nobody '; 
} 
Return "Hello," + name; 
} 
The public void UpdateData (String data) { 
System.out.println (data +) has been updated. "); 
} 

(2) Refer to instance 1 to publish this method as a service.

(3) Write client code call WebService (main reference [5])

The biggest difference between this example and the other examples is that other examples generally need to generate a client stub based on the service WSDL just now, and then invoke the service via stub, which is relatively simple, and the client must need stub stubs to access the service. In this example, the client does not use stub, but rather a common invocation method that accesses the service without any client stubs. You only need to specify the Web servce address, operation name, parameter, and function return type for. The code looks like this:

Helloservicetest2.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 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 operation name QName opqname = new QName (targetnamespace, opname); 
Set return value//class<?>[] Opreturn = new class[] {opreturntype}; The parameters passed in the operation need to be given in the argument, where the return Serviceclient.invokeblockin is called directly in the methodG (Opqname, Opargs, Opreturntype); /** * @param args * @throws axisfault * @throws classnotfoundexception/public static void Main (string[) args) t Hrows Axisfault, ClassNotFoundException {//TODO auto-generated method stub final String endpointreference = "http://l 
Ocalhost: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 to see that the output of the console port is: Hello, my Friends, which indicates that the client call succeeded. The biggest difference and advantage of this example is represented by the way the client calls, or the way in which the service is invoked, although the code is slightly more than the client stub stub, but this approach is uniform, does not need to produce stub stub code, solves the client has many kinds of problems. If the reader encapsulates the code further, I would like to invoke the method simply by passing the relevant parameters, which better illustrate the advantages of the service invocation. And this way is more simple and clear, a look will know the specific meaning. Without the need for some mechanism of stub classes.

(4) Overwrite the client invoke service code

(3) mentioned in the client application code to write a slightly more complex, the above client invoke the service program to rewrite, a lot of simplicity. The code is as follows:

Helloservicetest.java 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 Helloservicetest {public static void main (String args[]) throws Axisfault {//using RPC to invoke WebService Rpcser 
Viceclient serviceclient = new Rpcserviceclient (); 
Options options = Serviceclient.getoptions (); Specifies the URL that invokes WebService endpointreference Targetepr = new EndpointReference ("http://localhost:8080/WebServiceTest1/ 
Services/helloservice "); 
Options.setto (TARGETEPR); Specifies the namespace of the Sayhellotoperson method and WSDL file to invoke QName Opaddentry = new QName ("http://webservice.sjtu.edu", " 
Sayhellotopersonnew "); 
Specifies the parameter value of the Sayhellotoperson method object[] Opaddentryargs = new object[] {"Xuwei"}; 
Specifies the Class object for the data type of the Sayhellotoperson method return value class[] classes = new class[] {string.class}; Invokes the Sayhellotoperson method and prints the return value of the method System.out.println (serviceclient.invoKeblocking (Opaddentry,opaddentryargs, classes) [0]);  } 
}

Through the above content to introduce you to the Eclipse+webservice simple development examples, I hope to help you!

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.