4. Use axis2's auxiliary tools to publish and call WebService

Source: Internet
Author: User

This time, we edit a returned User object, List <User>, Map <String, User>, and User [], and use axis2 to publish WebService. No longer copy the class to the axis2 project directory.

Let's take a look at the WebService code on the server:

Code package com. hoo. service; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import java. util. random; import com. hoo. entity. user;/*** <B> function: </B> transmits the WebService * @ author hoojo * @ createDate 03:50:06 * @ file ManagerUserService for the List, Map, User, and Array data methods of the User object type. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class ManagerUserService {/*** <B> function: </B> transmits the User object, return the User object * @ author hoojo * @ createDate 2011-1-13 03:54:36 * @ param user * @ return User */public User editUser (User user) {user. setId (new Random (). nextInt (100); return user;}/*** <B> function: </B> returns List & lt; User & gt; * @ author hoojo * @ createDate 2011-1-13 03:57:19 * @ param j * @ return List & lt; User & gt; */public List <User> getUsers (int j) {List <User> users = new ArrayList <User> (); for (int I = 0; I <j; I ++) {User user User = new User (); user. setAddress ("china"); user. setEmail ("hoojo_@126.com"); user. setName ("hoojo"); user. setId (22 + I); users. add (user) ;}return users ;}/ *** <B> function: </B> Returns Map & lt; String, User & gt; type data * @ author hoojo * @ createDate 2011-1-13 04:03:41 * @ param j * @ return Map & lt; String, User & gt; */public Map <String, user> getUser4Map (int j) {Map <String, User> users = new HashMap <String, User> (); for (int I = 0; I <j; I ++) {User user = new User (); user. setAddress ("china"); user. setEmail ("amy@223.com"); user. setName ("amy"); user. setId (22 + I); users. put ("user #" + I, user);} return users;}/*** <B> function: </B> return the array data of the User * @ author hoojo * @ createDate 2011-1-13 04:05:23 * @ param j * @ return User [] */public User [] getUser4Array (int j) {User [] users = new User [j]; for (int I = 0; I <j; I ++) {User user User = new user (); User. setAddress ("china"); user. setEmail ("tom@223.com"); user. setName ("tom"); user. setId (22 + I); users [I] = user;} return users ;}}

AThe following uses axis2 to generate the aar file and then publish the WebService.

1. Click WorkSpace, right-click New, and select Other.

2. Select Axis2 Service Archiver and click Next

3. Click Browser to select the classes directory of your current project.

Click Next

4. If there is no WSDL, select the first item, click Next, and then click Next again.

5. Directly Next without setting service. xml

6. Enter the WebService Name and set the class path. Click Load to view the current class method.

Indicates that the method will be exposed to the method that can be called by the client, and then click Next

7. Set the name and directory of the aar File

Click Finish and you will see the successfully prompt.

8. Refresh the current project and you can see the ManangerUserService. aar file.

9. Visit the address http: // localhost: 8080/axis2/in the browser/

Click the Admin link. the user name and password are displayed in

In axis2.xml under Tomcat_Home \ webapps \ axis2 \ WEB-INF \ conf

<Parameter name = "userName"> admin </parameter>

<Parameter name = "password"> axis2 </parameter>

10. Enter the password to go to the Management page.

The following describes the common functions

Upload Service, of course, is used to Upload the aar file and release the WebService tool.

Available Service is used to view details of the currently released Service.

Available Service Groups is a Service group

Global Chain is a Global Chain that can be used.

Operation Specific Chains is the chain of a Specific Operation

Deactivate Service cancels a Service

Activate Service is used to release the canceled Service.

Edit Parameters is used to modify Parameters of the WebService class.

11. Here we use the Upload Service tool, and then select the axis2 tool to generate the aar file.

In this step, we can directly copy the ManagerUserService. aar file

Tomcat-home \ webapps \ axis2 \ WEB-INF \ services Directory.

12. Click "Available Service" to view the Service "ManagerUserService ".

BNow we need to use axis2 to generate the wsdl file.

1. Click WorkSpace, right-click New, and select Other. Select the client code generation tool

2. Click Next. You can see that you can choose wsdl. Select the first one and click Next.

The above shows how to generate a wsdl File Based on the Java class code.

3. Enter the classpath of your WebService and select the class directory of the project where your current class is located.

Click Add Folder to select the classes or bin directory of your project, that is, the directory where the class you filled in is located, and then click Test to Test whether it is correct.

Click Next to continue the Next step

4. You can see the name and namespace of the WebService to be released.

5. Continue to Next and select the wsdl file storage directory.

In this way, the services. wsdl file is generated.

CUse axis2 to generate the client call code

1. Right-click workspace, click New, select Other, and then select the aixs2 code generation tool.

2. Select the first item, generate Java code based on the wsdl file, and then click

3. Select the generated wsdl file.

If you publish your WebService to tomcat, you can access it through WebBrowser.

Http: /localhost: 8080/axis2/

Click the Services link to view your WebService. Click your WebService

You can also see the wsdl content.

In this way, you can enter:

Http: // localhost: 8080/axis2/services/ManagerUser? Wsdl

This address is also available.

4. Click Next to see the options for generating code for the specified method.

Note that when you select PortName, different options will produce different calling codes. The calling method is also slightly different. But you can see the generated source code for a detailed understanding.

5. Click Next, select the directory to save the code output, and click Finish.

After refreshing the directory, two files are added.

D,Write client code

Code package com. hoo. service; import java. rmi. remoteException; import com. hoo. service. managerUserStub. editUserResponse;/*** <B> function: </B> the ManagerUserService client call code * @ author hoojo * @ createDate 03:17:31 * @ file ManagerUserServiceClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class ManagerUserServiceClient {public static void main (String [] args) throws RemoteException {String target = "http: // localhost: 8080/axis2/services/ManagerUser"; ManagerUserStub stub = new ManagerUserStub (target);/*** call the User Bean mode: * EditUser and User are internal static classes. axis2 will encapsulate parameters */ManagerUserStub. editUser editUser = new ManagerUserStub. editUser (); ManagerUserStub. user u = new ManagerUserStub. user (); u. setAddress ("china"); u. setEmail ("axis2@axis2.com"); u. setName ("axis2"); u. setId (222); editUser. setUser (u); // The returned value is also encapsulated EditUserResponse eur = stub. editUser (editUser); ManagerUserStub. user returnUser = eur. get_return (); // returnUser. getId () dynamically resets System on the server. out. println (returnUser. getId () + "#" + returnUser. getName () + "#" + returnUser. getEmail () + "#" + returnUser. getAddress ();/*** User array mode */ManagerUserStub. getUser4Array userArray = new ManagerUserStub. getUser4Array (); userArray. setJ (3); ManagerUserStub. getUser4ArrayResponse userArrResponse = stub. getUser4Array (userArray); ManagerUserStub. user [] userArr = userArrResponse. get_return (); for (ManagerUserStub. user user: userArr) {System. out. println (user. getId () + "#" + user. getName () + "#" + user. getEmail () + "#" + user. getAddress ();}/*** Map User mode. Map, List * // * ManagerUserStub is not supported. getUser4Map userMap = new ManagerUserStub. getUser4Map (); userMap. setJ (3); ManagerUserStub. getUser4MapResponse userMapResponse = stub. getUser4Map (userMap); ManagerUserStub. map map = userMapResponse. get_return (); System. out. println (map); * // *** GetUsers is the internal class of ManagerUserStub. The Code Generation Tool of axis2 will encapsulate the content of the more wsdl file. * // * ManagerUserStub. getUsers getUsers = new ManagerUserStub. getUsers (); getUsers. setJ (3); // The server parameter int jManagerUserStub. getUsersResponse usersResponse = stub. getUsers (getUsers); System. out. println (usersResponse. get_return ());*/}}

After running, it is found that Map and List are not supported and data types cannot be read. The Wsdl file contains anyType, which may need to be configured to describe the return type. [To be followed up and solved]

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.