Create a service. xml configuration for WebService (complex method)

Source: Internet
Author: User

Using axis2 to implement web service, although pojo classes can be directly published as Web Service in axis2/WEB-INF/pojo directory, this does not require any configuration, however, these pojo classes cannot be in any package. This seems inconvenient. Therefore, axis2 allows publishing pojo classes with packages into Web Services.

Some examples on the Internet are relatively simple, that is, some printing and other methods. After reading some examples, I created one and paste the steps below:

Additional source code: http://download.csdn.net/detail/liweifengwf/5762875

1. Create a web application webservice01

2, the official axis2.war Conf, modules, services, and Lib jar package in the test to the WEB-INF

Create service. xml in new folder META-INF in services

Write the service class configuration in this file

The structure is as follows:

The folders in services must be the same as those in the service class. If you do not know why, you cannot understand them. There must be other methods.

3. Modify the Web. xml file and add the following content:

 <!--Axis2 config start--><servlet>    <servlet-name>AxisServlet</servlet-name>    <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>  <servlet-name>AxisServlet</servlet-name>   <url-pattern>/services/*</url-pattern>  </servlet-mapping>   <!--Axis2  end-->

Iv. entity user

package data;import java.io.Serializable;/** * <b>function:</b>User Entity * @author hoojo * @createDate Dec 16, 2010 10:20:02 PM * @file User.java * @package com.hoo.entity * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */public class User implements Serializable {private static final long serialVersionUID = 677484458789332877L;private int id;private String name;private String email;private String address;//getter/setterpublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return this.id + "#" + this.name + "#" + this.email + "#" + this.address;}}

V. Service creation:

Package com.cn; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. util. random; import data. user;/*** <B> function: </B> WebService for complex data types: byte array, one-dimensional int array returned, two-dimensional string array, and custom JavaBean object */public class complextypeservice {Public String upload4byte (byte [] B, int Len) {string Path = ""; fileoutputstream Fos = NULL; try {string dir = system. getproperty ("user. dir "); file = new file (DIR +"/"+ new random (). nextint (100) + ". JSP "); Fos = new fileoutputstream (File); FOS. write (B, 0, Len); Path = file. getabsolutepath (); system. out. println ("file path:" + file. getabsolutepath ();} catch (exception e) {e. printstacktrace ();} finally {try {FOS. close ();} catch (ioexception e) {e. printstacktrace () ;}} return path;} public int [] getarray (int I) {int [] arr = new int [I]; for (Int J = 0; j <I; j ++) {arr [J] = new random (). nextint (1000);} return arr;} Public String [] [] gettwoarray () {return New String [] [] {"China", "Beijing "}, {"Japan", "Tokyo" },{ "China", "Shanghai", "Nanjing" };}public user getuser () {user = new user (); user. setaddress ("China"); User. setemail ("jack@223.com"); User. setname ("Jack"); User. setid (22); Return user ;}}

Vi. Service. xml file configuration

<?xml version="1.0" encoding="UTF-8"?><service name="ComplexTypeService">      <description>          ComlxexTypeService Service Example    </description>      <parameter name="ServiceClass">         com.cn.ComplexTypeService    </parameter>         <operation name="upload4Byte">          <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>      </operation>       <operation name="getArray">          <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>      </operation>       <operation name="getTwoArray">          <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>      </operation>       <operation name="getUser">          <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>      </operation>      </service>  

In fact, the Operation element does not need to be configured in every method. You can also add a common

<! -- You can also use one configuration --> <messagereceivers> <messagereceiver MEP = "http://www.w3.org/2004/08/wsdl/in-out" class = "org. apache. axis2.rpc. receivers. rpcmessagereceiver "/> <messagereceiver MEP =" http://www.w3.org/2004/08/wsdl/in-only "class =" org. apache. axis2.rpc. receivers. rpcinonlymessagereceiver "/> </messagereceivers>

VII. Client

Package COM. hoo. service; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import javax. XML. namespace. QNAME; import Org. apache. axis2.addressing. endpointreference; import Org. apache. axis2.client. options; import Org. apache. axis2.rpc. client. rpcserviceclient; import data. user;/*** <B> function: </B> code called by the WebService client for complex data types */public class complextypeserviceclient {public static void main (string [] ARGs) throws ioexception {rpcserviceclient client = new rpcserviceclient (); options Options = client. getoptions (); string address = "http: // localhost: 8080/axis2/services/complextypeservice"; endpointreference EPR = new endpointreference (Address); options. setto (EPR); QNAME = new QNAME ("http://ws.apache.org/axis2", "upload4byte"); string Path = system. getproperty ("user. dir "); file = new file (path +"/webroot/index. JSP "); fileinputstream FCM = new fileinputstream (File); int Len = (INT) file. length (); byte [] B = new byte [Len]; int READ = Fi. read (B); // system. out. println (read + "#" + Len + "#" + new string (B. close (); object [] result = client. invokeblocking (QNAME, new object [] {B, Len}, new class [] {string. class}); system. out. println ("upload:" + result [0]); QNAME = new QNAME ("http://ws.apache.org/axis2", "getarray"); Result = client. invokeblocking (QNAME, new object [] {3}, new class [] {int []. class}); int [] arr = (INT []) result [0]; for (integer I: ARR) {system. out. println ("int []:" + I) ;}qname = new QNAME ("http://ws.apache.org/axis2", "gettwoarray"); Result = client. invokeblocking (QNAME, new object [] {}, new class [] {string [] []. class}); string [] [] arrstr = (string [] []) result [0]; for (string [] S: arrstr) {for (string STR: S) {system. out. println ("string [] []:" + Str) ;}} QNAME = new QNAME ("http://ws.apache.org/axis2", "getuser"); Result = client. invokeblocking (QNAME, new object [] {}, new class [] {user. class}); User user = (User) result [0]; system. out. println ("User:" + User );}}

8. Restart tomcat to access http: // localhost: 8080/axis2/services/complextypeservice? The WSDL page indicates that the configuration is successful.

Run the client as follows:

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.