Use XFire to compile Web Service on MyEclipse 5.1GA

Source: Internet
Author: User
Tags jboss server

From: http://www.blogjava.net/liyingcheng/archive/2007/10/24/155602.html

 

Development Environment
Sun Java 5 + Eclipse 3.2 + MyEclipse 5.0.0.

Overview 

This article describes how to use MyEclipseWeb Service to quickly develop and test a HelloWorld Web Service, including:
1. introduced the basic concepts of MyEclipse Web Services
2. Create a MyEclipse Web Services Project
3. Create a simple HelloWorld WebService
4. Deploy a Web Service in a Web container, for example, Tomcat 5.
5. Use Web Services Explorer to test the deployed Web Services

XFire Java SOAP framework Overview
MyEclipse Web Services is built on the XFire external oap framework and tools, and XFire is an open-source external oap framework. It has a lightweight information processing module that combines with SOAP information through STAX. Provides a simple API that works under Web Services and supports POJO and schema development. XFire supports Web Services standards, Spring integration, JBI, JAXB, XMLBeans, Java 5 and JAX-WS. it also supports HTTP, JMS, XMPP, and In-memory transmission protocols.

1. Create a WebService Project 

In MyEclipse 5.0, a new Project type is introduced, that is, the Web Service Project. This Project extends the MyEclipseWeb Project to support additional Web Service configuration, development, and deployment. this section uses Web Services Project wizard to create and configure a new Web Service Projects Web Service Wizard:
Create a MyEclipse J2EE Web Project
Configure XFire Servlet in the web. xml file of the project
Create XFire services. xml configuration file
Add the MyEclipse-XFire class library to the build path of the project.
Add a specified MyEclipse web project builder to the. projects file to deploy the services. xml file to its appropriate location. For example: <webroot>/WEB-INF/classes/META-INF/xfire/

Run Web Service Project Wizard
The wizard consists of three pages, Page-1 collects Web Project configuration details, Page-2 collects XFire configuration details, and Page-3 configures the XFire class library in the new build path.
1. Execute Web Services ProjectWizard.
1). Select File> New> Other.
2). Expand the MyEclipse categories according to the types of J2EE Projects
3) Select Web ServicesProject and click Next,

 

Figure-1: New Project Wizard Launcher
2. Add the Project Name and click Next.

 

Figure-2: Page-2, Collecting web configuration details
3. Add the configuration information of the XFire servlet and service. xml files to Page-2 in the Wizard. Follow the default values.

 

Figure-3: XFire servlet and services. xml configuration
4. in the Wizard Page-3, select the class Library to add to the project's build path. XFire Core Library is required. If you want to develop a client application in the project, XFire HTTP ClientLibraries is also required.

 

Figure-4: Selecting XFire libraries to add to new webservice project buildpath

5. Select Finish to complete the Web Service Project creation process.
Figure 5 shows the organizational structure of the newly created HelloWorld WebService project. The Web Service Project is similar to a standard MyEclipse Web project. The Red Area of the XFire Web Service configuration element is displayed:

 

Figure-5: Web services artifacts of a new web serviceproject

2. Create Web Service-Code-first Strategy
In this section, we will use MyEclipse Web Service Wizard and use Code-first Strategy to create a HelloWorldService example.

1. Execute MyEclipse Web Service Wizard. There are two methods to execute MyEclipse Web Service Wizard
Method 1. Execute the wizard from MyEclipseperspective toolbar
Select the new Web Service button from workbench on the Web Service Wizard that is opened
Method 2. Run the wizard from workbenchmenubar
1. Choose File> New> Other> MyEclipse> Web Service from workbench menubar.

 

Figure-6: Launching Web Service Wizard

2. On Page-1, select the HelloWorld project and select Create web service from Javabean.
3. Select Next to Page-2.

 

Figure-7: Page-1 of new web service wizard

4. Enter the Web Service name HelloWorldService
5. Select Java Source folder or select New to create a New source folder.
6. Fill in the Java package or select an existing package by selecting the Browse button. You can also select the New button to create a New Java package.
Note: The default value of the class executed for the Service interface and Service is generated based on the Web Service name entered.
7. Select Next to initialize the Web Service creation process.

 

Figure-8: Page-2 of new web service wizard.
This wizard generates the IHelloWorldServiceJava interface and HelloWorldServiceImpl Java class. and in services. in the xml configuration file, a <service> entity () is created. Note that the example (String message) method is generated in the interface class. After the Web Service is deployed, it is used as a test operation.

 

Figure-9: Newly created HelloWorld interface andimplementation class

Web ServiceServer service interface code:

Package org. Stephen cat. ws;
// Generated by MyEclipse

Public interface ISampleService {

Public String example (String message );

}

Web ServiceServer service implementation code:

Package org. Stephen cat. ws;
// Generated by MyEclipse

Public class SampleServiceImpl implements ISampleService {

Public String example (String message ){
Return message + "This is Xfire !! ";
}

}


3. Deploy a Web Service Project 
The Web Service can be deployed on any J2EE Application Server supported by MyEclipse. This section demonstrates how to deploy the HelloWorld project on the Tomcat5 Web container.

3.1 deploy HelloWorld Web Service Project

Figure-10 shows how to deploy the HelloWorld application.
1. Select the Deployer button from Server Manager (step-1)
2. In the Server Deployments dialog box, select Add to create a new deployment (step-2)
3. In the New Deployment dialog box, select the HelloWorld project and select the Exploded Archive option.
4. In the New Deployment dialog box, select Finish to package HelloWorld as WAR in Tomcat5's default position.
5. Select OK to complete deployment.

 

Figure-10: Three-step deployment process
The new HelloWorld WAR deployment will appear under Tomcat 5 nodes in the Server Manager view.

 

Figure-11: Servers Manager View depicting deployedHelloWorld web service project

4. Start the Tomcat server (Or JBoss Server)

5.Use WebService Explorer to test Web Service 
MyEclipse provides a Web Service Explorer to test the Web Service.
1. Select toolbar button in MyEclipse perspective to execute Web Service Explorer

 

Figure-15: Web Services Explorer launch button on workbenchtoolbar

2. Select the WSDL mode (Figure-16)
3. Type HelloWorldService in the activity panelWSDLDocument URL

Http: // localhost: 8080/wstest/services/SampleService? WSDL

Web Service Explorer Downloads the automatically generated WSDL document, which is composed of XFireframework and generates a series of operations that can call the service.
4. Select the example operation in the activity panel and type HelloWorld in the in0 domain
5. Select the Go button to call the example operation on HelloWorldService. The result is displayed on the Status panel.

 

Figure-16: Web Services Explorer testingHelloWorldService

6. Create a Java Test Client 
XFire provides a dynamic proxy framework that can read the WSDL document and create potential message services so that Java classes can execute Web services. this part will write a Java Web Service client for HelloWorldWeb Service
6.1 add XFire class library Configuration
To enable the example client to run in the HelloWorld project, you need to add the XFireHTTP Client class library to the build path of the project.
1. In the Package Development E view, right-click the HelloWorld project and select Build Path> AddLibrary.
2. Select MyEclipse Libraries
3. Select XFire HTTP Client Libraries

 

Figure-17: Choosing the XFire HTTP Client Libraries
4. Select Finish.
6.2 create a HelloWorldClient class
Execute the Java Class Wizard to create the HelloWorldClient class

 

Figure-18: HelloWorldClient defined in New Java ClassWizard.

Web ServiceClient codeAs follows:

Package org. Stephen cat. ws;

Import org. codehaus. xfire .*;
Import org. codehaus. xfire. service .*;
Import org. codehaus. xfire. service. binding .*;
Import org. codehaus. xfire. client .*;

Import java.net .*;

Public class SampleClient {

/**
* @ Param args
*/
Public static void main (String [] args ){
Service srvcModel = new
ObjectServiceFactory (). create (ISampleService. class );
XFireProxyFactory factory =
New XFireProxyFactory (XFireFactory. newInstance (). getXFire ());
String url =
"Http: // localhost: 8080/wstest/services/SampleService ";
Try {
ISampleService srvc = (ISampleService) factory. create (srvcModel, url );
String result = srvc. example ("hello world ");
System. out. print (result );
} Catch (MalformedURLException e ){
E. printStackTrace ();
}

}

}

This Code defines the process of creating an XFireweb service proxy. This proxy supports the POJOIHelloWorldService interface.

Right-click Run As> Java Application or Debug As> JavaApplication to Run or Debug the program.

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.