Declaration: This article is the result of this source, please send me a letter in any non-commercial use.
I. Introduction
Currently, more and more systems start using Web Services. However, because Java and .NET implementation of Web Services, how to make seamless achievements are still difficult to call each other. Fortunately, due to the XML machine readability and the WSDL standards comply with both parties, we can use a tool to achieve rapid development. This article will show how to use the WSDL2JAVA tool to implement the Call of the Java client .NET Web Services. To facilitate development, this article will use Eclipse as a development platform to implement this function using the Eclipse WSDL2JAVA plugin.
2: System configuration
Operating system: Windows2000 SP4
Java environment: Eclipse2.1
Org.apache.axis.wsdl2java.eclipse_1.1-rc2.0.2.zip
Org.apache.axis_1.1.zip
.NET environment: Visual Studio.net
Three: implementation of the Addint method
Test the simplest WebServices call. Enter parameters and returns are all standard data types.
3.1. WebServices for .NET
1. Create a new ASP.NET Web Services project.
2. Add the following code to Service1.asmx:
[WebMethod]
Public Int Addint (Int i, int J)
{
Return I J;
}
3. Perform this project in Visual Studio.NET, test the Addint method in IE, and return the result. In this way, we are configured with a .NET Web Services.
3.2 Implementation of Java client
1. Unzip org.apache.axis.wsdl2java.eclipse_1.1-rc2.0.2.zip, org.apache.axis_1.1.zip These two files to D: / WSDL2JAVA directory.
2. Configure Eclipse in accordance with d: /wsdl2java/org.apache.axis.wsdl2java.eclipse_1.1.0.2/html.
3. Create a directory Work in Eclipse, right-click, use Import Web Services to add .NET WebServices, generate Java's customer agents. (See the instructions of the INDEX file)
4. Create a new Java file in the Work directory, name InvoknetService.java. code show as below:
Import chenxuguang.webservicestest. *;
Public class invokenetService {
Service1locator_locator;
Service1soap _LocalService;
Public InvoknetService ()
{
Try {
_Locator = new service1locator ();
_LocalService = _locator.getService1SoAP ();
}
Catch (Exception Any) {
Any.printStackTrace ();
}
}
// Call Addint WebMethod
Public void caladdint ()
{
Try {
INT i = _LocalService.Addint (1, 2);
System.out.println ("1 2 IS" i);
Catch (Exception Any)
{
Any.printStackTrace ();
}
}
5. InvokeService.java named InvokeService.java in the Wrok directory. code show as below:
Import chenxuguang.webservicestest. *;
Public class invokeservice {
Public static void main (String [] args) {
InvoknetService NetService = new invokenetService ();
NetService.calladdinT ();
}
}
6. Run InvokeService.java. The result is as follows:
1 2 IS 3
execution succeed!
Four: GetCustomer () method implementation
Customer is a class for customization to store customer information. This example will discuss classes that implement .NET neutrons defined in Java.
4.1 GetCustomer () WebMethod implementation of .NET Web Services
1. Newly built a class, named Customer.cs, the code is as follows:
Using system;
Namespace WebServiceAdd
{
[Serializable]
Public Class Customer
{
Public Customer ()
{
this.customerid = "testid";
This.customername = "testname";
}
Public String Customerid;
Public String Customername;
}
}
2. Add GetCustomer () WebMethod in Services1.asmx. code show as below:
[WebMethod]
Public Customer getCustomer (String ID)
{
Customer Customer = New Customer ();
Return Customer;
}
3. Testing getCustomer () returns in IE is as follows:
XML Version = "1.0" encoding = "UTF-8"?>
-
Customer>
The result is correct!
4.2 Implementation of Java client
1. Regenerate the Java client agent.
After the execution is successful, you can find more than three Java files in the project directory, namely _getcustomer.java, _getcustomerReSponse.java, Customer.java. Among them, Customer.java is the Java implementation of our Costomer class defined in .NET. 2. Add the following code to InvoKenetService:
Public void callcustomerService ()
{
Try {
_Getcustomer param = new _getcustomer ();
Param.SetId ("chen");
_GetCustomerReSponse Customerres = this._localservice.getcustom (param);
Customer Customer = Customerres.getGetcustomerResult ();
System.out.println ("ID IS:" Customer.getCustomerid ());
System.out.println ("Name IS:" Customer.getCustomerName ());
}
Catch (Exception Any)
{
Any.printStackTrace ();
}
}
3. Modify the code of the INVOKSERVICE MAIN method:
InvoknetService NetService = new invokenetService ();
NetService.callcustomerService ();
4. Execute InvokeService, the input results are as follows:
ID IS: TestID
Name IS: Testname
testing successfully!
Precautions:
(1) From the OOP's point of view, we should encapsulate the Customer class, assign values using the get / set property, not the class variables of public types. However, if the class uses the Get / set property and modifies the proprietary variable in the class, the data will not be sent correctly because the XML description object is not used in the binary representation in memory.
(2) From the perspective of Web Services, data is exchanged between the system and is stateless. Therefore, our return results should be a pure data type, not a class. We should pack the result in WebMethod and passed in string or other types. In the client, it is also the data returned, not the method thereof. Therefore, from this perspective, we return the result when we implement WebMethod should be a pure data type (or data dictionary), and should not be a complex class object.
Too long, write it back tomorrow. Later, how to deliver the XMLDocument type.