Manually configuring WebService Getting Started instance

Source: Internet
Author: User
Tags soap object model require wsdl

1.AXIS2 Introduction:

The Apache Axis2 project is a Java-language based Web Services system service and client implementation. To gain lessons from Apache Axis 1.0, Apache Axis2 provides a complete object model and modular architecture that makes it easy to add functionality and support a new Web services-related description and recommendation. Axis2 can make it easy for you to perform the following tasks:

(1). Send a SOAP message.

(2). Receive and process SOAP messages.

(3). Create a Web service from a common Java class.

(4). Use WSDL to establish implementation classes that implement services and clients.

(5). It's easy to get WSDL from a service.

(6). Send and receive SOAP messages with attachments.

(7). Establish or use a rest based Web service.

(8). Establish or use services from Ws-security, ws-reliablemessaging, ws-addressing, Ws-coordination and ws-atomic to gain advantage.

(9). When new recommendations appear, use AXIS2 's modular structure to easily add support to it.

2. Configure the AXIS2 environment:

(1). Download Axis2, go to http://ws.apache.org/axis2/download the latest version of Axis2, typically two zip files, such as Axis2-1.4.1-bin.zip (including all the jar files in Axis2) and Axis2-1.4.1-war.zip (used to publish WebService to a Web container).
(2). Unzip the Axis2-1.4.1-war.zip, put the Axis2.war files in the directory into the project publishing directory of the servlet container, take Tomcat as an example, put them in the "%tomcat%/webapps" directory and start Tomcat.

(3). In the browser address bar, enter the following url:http://localhost:8080/axis2/", should be able to enter the Axis2 welcome interface, click" Validate "if there is no error, then the AXIS2 environment has been configured well.

(4). Click "Administration", the initial username and password are: admin and Axis2, can also go to "%tomcat%/webapps/axis2/web-inf/conf/axis2.xml" under the user name and password modification.

3.POJO published as a simple example of AXIS2 WebService:

(1). Programming ordinary Java classes to copy the compiled ". Class" File to the Axis2 POJO publishing directory "%tomcat%/webapps/axis2/web-inf/pojo".
Note: If there is no Pojo directory under the directory, you can create the directory manually.

(2). The startup Tomcat,pojo is published as all public methods in the Webservice,java class are published as WebService operation.

Note: The Pojo class cannot declare a package using the Package keyword, AXIS2 is hot deployed by default.

4.POJO No configuration release Advanced:

Because package is not supported for AXIS2 Pojo Publishing, if you need to invoke methods of other classes in your class, you will need to copy the ". Class" File of the referenced class to the "%tomcat%/webapps/axis2/web-inf/classes" directory.

Write Pojo class User.java, note: 1, the network transmission of objects to serialize 2, no package name 3, first compiled into a. class file, in the Web-inf/pojo, without the directory, create their own

public class User implements java.io.Serializable {
	private Integer userId;			
	Private String username= "Hello World";		
	private String password;			

	Public Integer GetUserID () {return
		this.userid;
	}

	public void Setuserid (Integer userId) {
		This.userid = userId;
	}

	Public String GetUserName () {return
		this.username;
	}

	public void Setusername (String username) {
		this.username = username;
	}

	Public String GetPassword () {return
		this.password;
	}

	public void SetPassword (String password) {
		this.password = password;
	}
}


Restart Tomcat, open address: http://localhost:8080/axis2/services/listServices, you can see the published method

To write client test methods:

Package Samland.webservice.demo;

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 Test {public static void main (string[] args) {try {//call webservice:rpcserviceclient client = N Using RPC method
		EW rpcserviceclient ();
		Options options=client.getoptions (); Specifies the url:endpointreference targeturl that invokes WebService = new EndpointReference ("http://localhost:8080/axis2/services/
		User ");
		Options.setto (TargetUrl);
		Specifies the parameter value that invokes the service method://Specifies the incoming parameter object[] Parameters = new object[]{"Wangxiaoya"};
		Specifies the called namespace and Method (Setusername) QName QName = new QName ("Http://ws.apache.org/axis2", "setusername");
		
		When the invoked method does not return a value, the Invokerobust method Client.invokerobust (QName, parameters) of the Rpcserviceclient class should be used; 
		Class[] classes = new Class[]{string.class};
		QName qname2 = new QName ("User", "GetUserName"); When a method has no arguments, the second parameter value cannot be null, and theWith new object[]{} object[] parameters2 = new object[]{}; The first of the three arguments is the QName object, which represents the method name to invoke, the second parameter represents the parameter value of the//webservice method to invoke, and the third parameter represents the return value type of the WebService method Object RESULT2 =
		Client.invokeblocking (Qname2, Parameters2, classes) [0];
	System.out.println (RESULT2);
	catch (Axisfault e) {//TODO auto-generated catch block E.printstacktrace ();
 } 
}
}


Output results:

Hello World

Note: namespace mismatch error handling method

When WebService development deployment based on AXIS2 (using the Java Client Invoke service) may be reported to namespace mismatch require Http://ws.apache.org/axis2 found http:// Ws.apache.org/axis2/xsd such a mistake. According to my experience, when using Pojo this way to develop, QName parameters in the Http://ws.apache.org/axis2, Its parameters are http://service when based on Service.xml development.

Of course, when the namespace is out of error in the Java development environment, you can find similar information from the Debug window: Exception in Thread "main" Org.apache.axis2.AxisFault:namespace mismatch require Http://service found Http://ws.apache.org/axis2. Change the namespace to require. Search in your code, and then modify it.

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.