Eclipse+webservice Development Examples

Source: Internet
Author: User
Tags tomcat server

1. References:

1. Write a simple WebService instance 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, move the slider in the Web service type to the location of start Service and the slider in client type to the "Test client" location.


(6) On the right side of the Web Service type slider, there is a "Configuration", click on the options below it to enter the Service Deployment Configuration Object box, where you select the corresponding server ( I use Tomcat6.0 here) and the Web Service runtime (choose Apache Axis2), for example:


(7) Click OK, then return to the Web Service dialog box, the same, the Client type in the right side of the slider also has "Configuration", but also to the corresponding set, the same step. When you are finished, next---next. Into the Axis2 Web Service Java Bean Configuration, we select 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) After the start, click "Next-> Next", all by default, and finally, click Finish. Finally, for example, the following interface appears: (Web Service Explorer), where we can test our web services. (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 the WebService Explorer is turned off in eclipse and 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 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), appear, enter in the X Input box 1, enter the Y input box 2, click "Go , the result 3.0 is 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 a value of two, respectively 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};//calls the 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])

This sample and other examples of the biggest difference is here, the other sample generally need to base on the service WSDL just generated clientstub, and then through the stub to invoke the service, this way seems relatively single, the client must need stub stub to access the service, very not. The client of this sample does not use stubs, but rather a generic invocation that does not require any client stubs to access the service. 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 requires that the passed in parameters have been given in the argument, where the direct incoming method calls return Serviceclient.invokeblocking ( Opqname, Opargs, Opreturntype);} /** * @param args * @throws axisfault* @throws classnotfoundexception */public static void Main (string[] args) throws Axisfault,classnotfoundexception {//TOD O 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]);}}
To execute the program, click Run As->java application and be able to see that the output of the console port is: Hello, My Friends, indicating that the client call was successful. The most diverse and advantageous tables of the sample are now called by the client, or the way the service invocation is initiated, although the code is slightly more than the client stub stub, but in such a unified manner, there is no need to produce stub stub code, which overcomes the client's very many classes of problems. Given the reader's further encapsulation of the code, I would like to call it very easy and simply pass the relevant parameters, which better illustrates 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]);}}









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.