Web Service Development Instance (java code)

Source: Internet
Author: User

WebService is an advanced application, which is different from the Struts, Spring, Hibernate and other frameworks that we have learned before. WebService is a Service-Oriented Architecture (SOA) that looks larger than the SSH framework. So what is it? What is a service-oriented architecture?

 

Let's look at a requirement that the group company may have a variety of WEB applications. For example, an inventory system was developed in the previous year, an ERP system was developed last year, and an OA system was developed this year. Now the group company needs to integrate these three systems. Do they need to be recoded to integrate them? These three systems are written in different languages, which is a waste for the company. WebService can effectively solve this problem.

 

WebService is a solution that can integrate cross-language, cross-platform, and distributed systems. WebService is like a line to bring these systems together-the Enterprise Service Bus (ESB ). WebService uses Simple Object Access Protocol (SOAP) to transmit xml data using http protocol (xml is the most common and has other formats of data .) To complete the integration of the lace-up.

 

What is integration? Of course, it is the integration of functions and data, that is, one system can call the WebService Interface of another system to complete data interaction. In this way, we need to know which interfaces are exposed for applications that provide the WebService Service function. We can find out through the WebService Description document (WSDL. You do not need to write the WSDL manually. Java's WebService implementation can be automatically generated for us. JDK1.6 supports WebService, but it is not mature enough. Therefore, we use the WebService implementation provided by a third-party open-source Apache organization-Axis.

 

The current version of Axis is Java, and its C ++ version is under development. Axis is a powerful soap engine. Its details are not described here. Next, let's write an example program to understand the WebService application process.

 

1. Compile WEB applications that support WebService

1. Create a project

Project Name: TestWebService

Target runtime: Apache Tomcat V6.0

Other Default values

 

2. Add the Jar package of Axis

Add all jar files in the lib subdirectory of the jar package directory of axis to the project.

 

3. Configure web. xml

There is an axisWeb application in the webapps subdirectory of axis. We use its web. xml file content directly.

 

4. Add a function class

 

  1 package com.enfang;
2
3
4  public class TestLiu {
5
6 public String getInfo(String aa){
7 return "webservice:"+aa;
8 }
9 }
10  

 

5. Deploy WebService

 

We can copy the class files to be exposed to the WEB application directory and change the file suffix to jws. Rename the class file and place it in the WEB directory? This is not good, and it is exposed.

 

So here we use a constant deployment method to add a deploy. wsdd file to the project (root directory:

XML Code

  1 <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java=
2
3 "http://xml.apache.org/axis/wsdd/providers/java">
4 <service name="TestLiu" provider="java:RPC">
5 <parameter name="className" value="com.enfang.TestLiu"/>
6 <parameter name="allowedMethods" value="*"/>
7 <parameter name="scope" value="session"/><!-- request, session, or application -->
8 </service>
9  </deployment>
10  

 

Deploy the project on the Tomcat server and start the Tomcat server. Right-click the project and choose --> Run As... --> Run Configurations... to create a JavaApplication. On the main Page, specify project as our TestWebService, and MainClass as org. apache. axis. client. AdminClient. On the Arguments page, set Program arguments to-l http: // localhost: 8080/TestWebService/servlet/AxisServlet deploy. wsdd. Click Run.

 

After completing the above operations, AdminClient will generate a WEB-INF file for us to deploy in the server-config.wsdd directory of Tomcat's TestWebService, which is the configuration file that is provided to Axis.

 

6. Generate a client

Create a bat file in the root directory, for example, WSDL2Java. bat. The Code is as follows:

Ruby code

 set Axis_Lib=.\WebRoot\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=.\src
set Package=client.webservice
set wsdl_path=http://localhost:8080/TestWebService/services/TestLiu?wsdl
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% %wsdl_path%
pause

 

 

Then execute this file to generate the client. The client directory is in src/client/webservice.

7. Call the webservice Client

Java code

  1 package cn.anycall.test;
2  import java.rmi.RemoteException;
3
4  import javax.xml.rpc.ServiceException;
5
6  import client.webservice.*;
7  public class Test {
8
9 /**
10 * @param args
11 * @throws ServiceException
12 * @throws RemoteException
13 */
14 public static void main(String[] args) throws ServiceException, RemoteException {
15 // TODO Auto-generated method stub
16   TestLiuService service = new TestLiuServiceLocator();
17 TestLiu_PortType client = service.getTestLiu();
18 String aa = client.getInfo();
19 System.out.println(aa);
20 }
21
22 }
23
24

 

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.