Eclipse+webservice Development Examples

Source: Internet
Author: User

1. Reference:

1. Write a simple WebService example using Java http://nopainnogain.iteye.com/blog/791525

2.axis2 integrates with Eclipse to develop Web Service http://tech.ddvip.com/2009-05/1242968642120461.html

3.http://blog.csdn.net/lightao220/article/details/3489015

4.http://clq9761.iteye.com/blog/976029

5. Building a Web Services application using Eclipse+axis2+tomcat (example commentary)

2. Example 1 (see [2]) 2.1. System function: Develop a calculator service Calculateservice, this service includes plus (plus), minus (minus), multiply (multiply), except (divide) operation.

2.2. Preparation before development:

    1. Install Eclipse-jee.
    2. Download the latest version number of Axis2, URL http://axis.apache.org/axis2/java/core/download.cgi, select standard Binary distribution Zip package, Extract the resulting folder name axis2-1.4.1, the file structure within the folder such as the following:


2.3. Pre-development configuration:in the Eclipse's menu bar, window-----Preferences Web Service--Axis2perferences, in the AXIS2 runtime location, select the position of the Axis2 decompression package, set up, click "OK" to the right. ()


2.4. Developing Web Service:

(1) Create a new Java Project named "WebServiceTest1"
(2) Create a new class named "Calculateservice". Complete code such as the following:

Package edu.sjtu.webservice;/** * Calculator operation * @author Rongxinhua */public class Calculateservice {//Add public float plus (float x, float y) {return x + y;} Subtract public float minus (float x, float y) {return x-y;} Multiply public float multiply (float x, float y) {return x * y;} Division public float Divide (float x, float y) {if (y!=0) {return x/y;} elsereturn-1;}}
(3) on the "WebServiceTest1" project, new---and other, find "Web Service" under "Web Services";


(4) Next, in the Web Services object box that appears, click "Browse" in the service implementation to go to the Browse classes object frame, Find the Calculateservice class that we just wrote. (for example). Click "OK" to return to the Web Service session box.



(5) In the Web Service dialog box, place the slider in the Web service type. To the start service location, move the slider in client type to the location of the "Test client".


(6) On the right side of the Web service type slider, there is a "configuration" and click on the option below to enter the service Deployment configuration object frame. Select the corresponding server here (I use Tomcat6.0 here) and the Web Service runtime (choose Apache Axis2), for example:


(7) After you click OK, return to the Web Service dialog box. The right side of the slider in the Client type also has a "Configuration", which also has to be corresponding to the position. Step above. After the completion. Next---next.

Enter the Configuration of the Axis2 Web Service Java Bean. We chose generate a default services.xml. For example, as seen in:

(8) To the Server Startup dialog box, there is a button "Start Server" (for example), click on it, you can start the Tomcat server.

(9) Wait until after the start. Click "Next-> Next". Everything is done by default. Finally, click Finish. At last. such as the following interface appears: (Web Service Explorer). We can test our web services here.

(Use a browser to open words such as the following address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3). For example, as seen in:


Note: Open the Web Service Explorer in the browser (sometimes WebService Explorer is turned off in eclipse.) Can be opened in such a way)

First login address: http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp.

Then select the Web Service exoplorer tag in the top right corner of the webpage. Then enter the WSDL address: http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl. This WSDL address is the WSDL of the service we just announced. Click Go, for example to see:


You can then see, for example, the following interface:


(10) The test is relatively simple, for example, we choose a "plus" operation (must be calculateservicesoap11binding). appears, enter 1 in the input box of X. Enter 2 in the input box for Y. Click "Go" and the result 3.0 will be displayed in the status bar. Other methods of testing are similar. The results are as seen.

2.5.CalculateServiceclient caller before we have defined the methods of subtraction and advertised them as services, what we need to do now is to invoke these services.

The client invoker, as seen 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//using RPC method call Webservicerpcservic Eclient serviceclient = new Rpcserviceclient (); Options options = Serviceclient.getoptions ();// Specifies the urlendpointreference Targetepr = new endpointreference called WebService ("http://localhost:8080/WebServiceTest1/ Services/calculateservice "); Options.setto (TARGETEPR); Specifies the method in the machine to be called and the namespace of the WSDL file: Edu.sjtu.webservice.

QName opaddentry = new QName ("http://webservice.sjtu.edu", "plus");//addition 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//Specify the plus method with two values, each addend and Addend object[ ] Opaddentryargs = new object[] {};//Specifies the Class object of the data type of the plus method return value class[] classes = new class[] {float.class};//call Plus Method and outputs 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]);}}

Execution Result:
3.0-1.02.00.5
3. Example 2. HelloService

(1) First define the service method, the code as seen below:

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;} public void UpdateData (String data) {System.out.println (data +) has been updated. ");}}
(2) Example 1 This method is advertised as a service.

(3) Writing the client code call WebService (main references [5])

In this case, the most important difference between the sample and other examples is that in this case, the other samples generally need to generate clientstub based on the service WSDL just now. The service is then invoked through the stub. Such a way appears to be relatively single. The client must require stub stubs to access the service. Very non-facet. In this example, the client does not use stubs, but rather a common invocation method. There is no need to access the service without any client stubs. It is only necessary to specify the Web servce address, operation name, number of parameters, and function return type. The code looks like the following:

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 ;p rivate 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[] {The Opreturntype};//operation needs to pass in the number of parameters given in the argument. Here the direct incoming method calls return Serviceclient.invokeblocking (Opqname, Opargs, Opreturntype);} /** * @param args * @throws axisfault * @throws classnotfoundexception */public static void Main (string[] args) throws Axi sfault,classnotfoundexception {//TODO auto-generated method stubfinal 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, opNam E, Opargs,opreturntype); System.out.println (((string[) response[0]) [0]);}}

Execute the program. Click Run As->java Application. The output that can see the console port is: Hello, My Friends. Indicates that the client call succeeded. The most diverse and advantageous tables of the sample are now called by the client. Or a way to initiate a service invocation. Although the code is slightly more than the client stub, it is uniform in this way. There is no need to produce stub stub code, which overcomes the problem of very many kinds of clients. Suppose the reader further encapsulates the code. I want to call the way very easy. The only way to pass the relevant parameters is to better illustrate the benefits of service invocation. And this way is more simple and clear, a look at the detailed meaning. Without the need to make some mechanism of the stub class.


(4) Rewriting the code of the client invocation service

(3) mentioned in the client application code is slightly more complicated to write, the following will call the above Client service program to rewrite, a lot more concise. 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 call Webservicerpcserviceclient serviceclient = new Rpcserviceclient (); Options options = Serviceclient.getoptions ();//Specifies to call WebService's urlendpointreference Targetepr = new EndpointReference ("http:// Localhost:8080/webservicetest1/services/helloservice "); Options.setto (TARGETEPR);// Specifies the Sayhellotoperson method to invoke and the namespace of the WSDL file QName opaddentry = new QName ("http://webservice.sjtu.edu", "sayhellotopersonnew ");//Specify the Sayhellotoperson method's object[] Opaddentryargs = new object[] {" Xuwei "};// Class object specifying the data type of the Sayhellotoperson method return 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]);}}









Eclipse+webservice Development Examples

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.