Eclipse + WebService Development Instance

Source: Internet
Author: User
ArticleDirectory
    • 2. 1. system functions:
    • 2. 3. Pre-Development Configuration:
    • 2. 4. Develop Web Service:
    • Note: Open Web Service explorer in the browser (sometimes WebService explorer is disabled in eclipse, which can be opened in this way)
    • 2.5.calculateservice client caller
    • Helloservicetest2.java
    • Helloservicetest. Java
1. References:

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

2. axis2 integrated development of Web Service http://tech.ddvip.com/2009-05/1242968642120461.html with eclipse

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

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

5. Use eclipse + axis2 + Tomcat to build Web services applications (for example)

2. instance 1 (mainly see [2]) 2. 1. system Function: Develop a calculator service calculateservice, which includes plus, minus, multiply, and divide operations.

2. Preparations before development:

    1. Install eclipse-jee;
    2. Download the latest version of axis2, URL http://axis.apache.org/axis2/java/core/download.cgi, select the standard binary distribution zip package, decompress the directory name axis2-1.4.1, the file structure in the directory is as follows:

2. 3. Pre-Development Configuration: In the menu bar of Eclipse, window --> preferences --> Web Service --> axis2 Perferences: In the axis2 runtime location, select the location where the axis2 package is decompressed. After setting it, click "OK. ( )

2. 4. Develop Web Service:

(1) Create a New Java project named "webservicetest1"
(2) create a new class named "calculateservice", completeCodeAs follows:

Package Edu. SJTU. webService; <br/>/** <br/> * calculator calculation <br/> * @ author rongxinhua <br/> */<br/> public class calculateservice {<br/> // addition <br/> public float plus (float X, float y) {<br/> return X + Y; <br/>}< br/> // subtraction <br/> public float minus (float X, float y) {<br/> return x-y; <br/>}< br/> // multiplication <br/> public float multiply (float X, float y) {<br/> return x * Y; <br/>}< br/> // Division <br/> public float divide (Float X, float y) {<br/> If (y! = 0) <br/>{< br/> return x/y; <br/>}< br/> else <br/> return-1; <br/>}< br/>}(3) On the "webservicetest1" Project New --> Other, find "Web Service" under "Web Services ";

(4) Next, In the displayed web services object box, click "Browse" in service implementation to enter the Browse classes object box, find the calculateservice class we just wrote. (For example ). Click "OK" to return to the Web Service dialog box.

(5) In the Web Service dialog box, adjust the slider in the Web service type to the "Start Service" position and the slider in the client type to the "test client" position.

(6) There is a "configuration" on the right of the web service type slider. Click the options below it to enter the service deployment configuration object box, select the corresponding server (tomcat6.0 here) and Web Service Runtime (select Apache axis2), for example:

(7) after clicking OK, the system returns to the Web Service dialog box. Similarly, "configuration" also exists on the right of the slider in the client type, which must be set as follows. After completion, next --> next is the line. Go to axis2 Web Service Java Bean configuration, and select generate a default services. XML, as shown in:

(8) In the server startup dialog box, press start server (for example) and click it to start the Tomcat server.

(9) after the startup is complete, click "next --> next". Everything is done by default. Finally, click Finish. Finally, the following page is displayed: (Web Service Explorer). Here we can test our web service. (Use the following URL in a browser: http: // 127.0.0.1: 19189/WSE/wsexplorer. jsp? Org. Eclipse. WST. ws. Explorer = 3 ). As shown in:

Note: Open Web Service explorer in the browser (sometimes WebService explorer is disabled in eclipse, which can be opened in this way)

First, log on to http: // 127.0.0.1: 19189/WSE/wsexplorer. jsp. Then select the Web Service exoplorer tag in the upper-right corner of the page. Enter the WSDL address: http: // localhost: 8080/webservicetest1/services/calculateservice? WSDL. The WSDL address is the WSDL of the service we just released. Click Go, as shown in:

Then you can see the following interface:

(10) The test is relatively simple. For example, we select a "plus" Operation (must beCalculateservicesoap11binding), Appears, enter 1 in the input box of X, enter 2 in the input box of Y, and click "go". The result 3.0 is displayed in the Status column. The tests for other methods are similar. Shows the result.

2.5.calculateservice client call Program Previously, we have defined the methods for addition, subtraction, multiplication, division, and released these methods as services. Now we need to call these services. The client call program is shown in the following code: calculateservicetest. Java Package Edu. SJTU. webService. test; </P> <p> Import javax. XML. namespace. QNAME; <br/> Import Org. apache. axis2.axisfault; <br/> Import Org. apache. axis2.addressing. endpointreference; <br/> Import Org. apache. axis2.client. options; <br/> Import Org. apache. axis2.rpc. client. rpcserviceclient; </P> <p> public class calculateservicetest {</P> <p>/** <br/> * @ Param ARGs <br/> * @ throws axisfault <br/> */<br/> Public static Void main (string [] ARGs) throws axisfault {<br/> // todo auto-generated method stub </P> <p> // call WebService in RPC mode <br/> rpcserviceclient serviceclient = new rpcserviceclient (); <br/> options Options = serviceclient. getoptions (); <br/> // specify the URL to call WebService <br/> endpointreference targetepr = new endpointreference (<br/> "http: // localhost: 8080/webservicetest1/services/calculateservice "); <br/> options. se Define (targetepr); </P> <p> // specify the method in the calculation machine to be called and the namespace of the WSDL file: edu. SJTU. WebService. <Br/> QNAME opaddentry = new QNAME ("http://webservice.sjtu.edu", "plus"); // addition <br/> QNAME opaddentryminus = new QNAME ("http://webservice.sjtu.edu ", "minus"); // subtraction <br/> QNAME opaddentrymultiply = new QNAME ("http://webservice.sjtu.edu", "Multiply "); // multiplication <br/> QNAME opaddentrydivide = new QNAME ("http://webservice.sjtu.edu", "divide"); // Division <br/> // specify two parameter values for the plus method, add and add respectively <br/> object [] opaddentryargs = new object [] {1, 2 }; <br/> // specifies the Class Object of the Data Type returned by the plus method <br/> class [] classes = new class [] {float. class };< br/> // call the plus method and output the return value of the method <br/> system. out. println (serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0]); <br/> system. out. println (serviceclient. invokeblocking (opaddentryminus, opaddentryargs, classes) [0]); <br/> system. out. println (serviceclient. invokeblocking (opaddentrymultiply, opaddentryargs, classes) [0]); <br/> system. out. println (serviceclient. invokeblocking (opaddentrydivide, opaddentryargs, classes) [0]); </P> <p >}< br/>}Running result: <textarea readonly name="code" class="java">3.0 <br/>-1.0 <br/> 2.0 <br/> 0.5</textarea> 3. instance 2. helloservice

(1) first define the service method. The Code is as follows:

Package Edu. SJTU. webService; </P> <p> public class helloservice {<br/> Public String sayhellonew () {<br/> return "hello "; <br/>}</P> <p> Public String sayhellotopersonnew (string name) {<br/> If (name = NULL) {<br/> name = "nobody"; <br/>}< br/> return "hello," + name; <br/>}</P> <p> Public void updatedata (string data) {<br/> system. out. println (Data + "updated. "); <Br/>}< br/>}(2) refer to instance 1 to publish this method as a service.

(3) Compile client code to call WebService (refer to [5])

The biggest difference between this example and other examples is here. In other examples, you need to generate the client stub according to the service WSDL and then call the service through stub. This method is relatively simple, the client must use the stub to access the service. In this example, the client does not use the stub method, but is a universal call method that can access the service without any client stubs. You only need to specify the Web Servce address, operation name, parameter, and function return type. The Code is as follows:

Helloservicetest2.java

Package Edu. SJTU. webService. test; </P> <p> Import javax. XML. namespace. QNAME; </P> <p> Import Org. apache. axis2.axisfault; <br/> Import Org. apache. axis2.addressing. endpointreference; <br/> Import Org. apache. axis2.client. options; <br/> Import Org. apache. axis2.rpc. client. rpcserviceclient; </P> <p> public class helloservicetest2 {<br/> private rpcserviceclient serviceclient; <br/> private options; <br/> priva Te endpointreference targetepr; </P> <p> Public helloservicetest2 (string endpoint) throws axisfault {<br/> serviceclient = new rpcserviceclient (); <br/> Options = serviceclient. getoptions (); <br/> targetepr = new endpointreference (endpoint); <br/> options. setto (targetepr); <br/>}</P> <p> Public object [] invokeop (string targetnamespace, string opname, <br/> object [] opargs, class <?> [] Opreturntype) throws axisfault, <br/> classnotfoundexception {<br/> // set the operation name <br/> QNAME opqname = new QNAME (targetnamespace, opname ); <br/> // set the return value <br/> // class <?> [] Opreturn = new class [] {opreturntype}; <br/> // The parameter to be input for the operation is already specified in the parameter, call <br/> return serviceclient. invokeblocking (opqname, opargs, opreturntype ); <br/>}</P> <p>/** <br/> * @ Param ARGs <br/> * @ throws axisfault <br/> * @ throws classnotfoundexception <br/> */<br/> Public static void main (string [] ARGs) throws axisfault, <br/> classnotfoundexception {<br/> // todo auto-generated method stub <br /> Final string endpointreference = "http: // localhost: 8080/webservicetest1/services/helloservice"; <br/> final string targetnamespace = "http://webservice.sjtu.edu "; <br/> helloservicetest2 client = new helloservicetest2 (endpointreference); </P> <p> string opname = "sayhellotopersonnew "; <br/> object [] opargs = new object [] {"my friends" };< br/> class <?> [] Opreturntype = new class [] {string []. class };</P> <p> object [] response = client. invokeop (targetnamespace, opname, opargs, <br/> opreturntype); <br/> system. out. println (string []) response [0]) [0]); <br/>}</P> <p >}< br/>Run the program and click Run as-> JAVA application. The output of the Console port is hello and my friends, indicating that the client has successfully called the program. The biggest difference and advantages of this example are the client call method, or the method for initiating a service call. Although the code is slightly more than the client's Stub stub method, this method is uniform, there is no need to produce the stub code, which solves many class problems on the client. If the reader further encapsulates the code, I think the call method is very simple, just passing the relevant parameters, which better demonstrates the advantages of service calling. In addition, this method is simpler and clearer. Instead of getting some mechanisms of stub classes.

(4) rewrite the code for the client to call the service

(3)The client application code mentioned in is slightly complicated to write. Below we will rewrite the above client to call the service program, which is much simpler. The Code is as follows:

Helloservicetest. Java

Import javax. XML. namespace. QNAME; <br/> Import Org. apache. axis2.axisfault; <br/> Import Org. apache. axis2.addressing. endpointreference; <br/> Import Org. apache. axis2.client. options; <br/> Import Org. apache. axis2.rpc. client. rpcserviceclient; </P> <p> public class helloservicetest {<br/> Public static void main (string ARGs []) throws axisfault {<br/> // call WebService in RPC mode <br/> rpcserviceclient serviceclient = new rpcserviceclient (); <br/> options Options = serviceclient. getoptions (); <br/> // specify the URL to call WebService <br/> endpointreference targetepr = new endpointreference ("http: // localhost: 8080/webservicetest1/services/helloservice "); <br/> options. setto (targetepr); </P> <p> // specify the sayhellotoperson method to call and the namespace of the WSDL file <br/> QNAME opaddentry = new QNAME ("http://webservice.sjtu.edu ", "sayhellotopersonnew"); <br/> // specify the parameter value of the sayhellotoperson method <br/> object [] opaddentryargs = new object [] {"xuwei "}; <br/> // specifies the Class Object of the Data Type returned by the sayhellotoperson method. <br/> class [] classes = new class [] {string. class }; <br/> // call the sayhellotoperson method and output the return value of this method <br/> system. out. println (serviceclient. invokeblocking (opaddentry, opaddentryargs, classes) [0]); <br/>}< br/>}

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.