Use cxf2.0 to develop WebService under myeclipse blue edition 6.1

Source: Internet
Author: User
Tags webservice annotation

The previous article introduced how to install the cxf plug-in STP under myeclipse blue edition 6.1.

This time I will explain how to use this tool to quickly develop a simple WebService.

The cxf used in this article is a apache-cxf-2.0.5-incubator.

1. Set the STP plug-in:

Start myeclipse blue edition 6.1. Select menuWindow-> preferencesIn the left-side SOA tools branch of the dialog box, you can set the STP plug-in.

1. Add cxf Runtime

Select installed runtimes in the Branch and click Add;

Select Apache cxf 2.0 and next;

Browse to the extract directory of the apache-cxf-2.0.5-incubator and click Finish. Note that the JRE here is the JRE run by the plug-in. It is best to select Sun's jre1.5 (an error occurs when I select IBM JRE ).

2. Other settings

Select the JAX-WS and select Use Wizard;

Set the SOAP Protocol version for generating the WSDL file;

Click OK to complete the plug-in settings.

2. Develop WebService

You can use cxf to develop WebService in two modes: Java interface and WSDL file.

This section describes how to start development from the Java interface (the method for developing from the WSDL file. After reading this article, you will naturally understand it .)

1. Create a project

Select menuFile-> New-> project..., Select the JAX-WS Java first project under SOA tools;

Enter the project name, javafirst;

Select runtime;

Click Finish to create a project.

It is best to manually create a lib directory for the newly created Project, copy all cxf library files, and delete the libraries added by the wizard in the Project Build path, manually add the library in lib to the build path of the project. In addition, it is best to change the JRE.

2. Create a package

Select File> New> package to create a package named com. javafirst. PK.

3. Create an Interface

Under the package you just created, use file-> New-> interface to create an interface named hello.

The code in the interface is as follows:

Public interface hello ...{
Public String echo (string MSG );
}

4. Add WebService Annotation

In package explorer, select the interface you just defined and add it through the menu SOA-> JAX-WS-> Create web service;

Or right-click the interface and choose JAX-WS tools> Create web service from the right-click menu to add.

Retain the default parameters of the Wizard to generate WebService annotaion.

The code after annotation is added is as follows:

@ WebService (targetnamespace = "http://pk.javafirst.com/", name = "hello ")
Public interface hello ...{
Public String echo (string MSG );
}

5. After saving (select project> build automaticly in advance), the plug-in will automatically generate the WSDL file and a bunch of junk SRC files.

In this case, you need to edit the WSDL file. There are three places to modify:

A. In order to facilitate the release and the programmer's understanding, arg0 in the following code can be modified to the MSG defined in the interface. Note that once modified, it cannot be tested using the client generated later.

<Xs: complextype name = "Echo">
<Xs: sequence>
<Xs: Element minoccurs = "0" name = "arg0" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>

 

 

<Xs: complextype name = "Echo">
<Xs: sequence>
<Xs: Element minoccurs = "0" name = "MSG" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>

 

B. To be compatible with. net, replace parameters and so on.

<WSDL: Message name = "echoresponse">
<WSDL: part name = "Parameters" element = "TNS: echoresponse">
</WSDL: Part>
</WSDL: Message>
<WSDL: Message name = "Echo">
<WSDL: part name = "Parameters" element = "TNS: Echo"> </WSDL: Part>
</WSDL: Message>

 

<WSDL: Message name = "echoresponse">
<WSDL: part name = "echooutput" element = "TNS: echoresponse">
</WSDL: Part>
</WSDL: Message>
<WSDL: Message name = "Echo">
<WSDL: part name = "echoinput" element = "TNS: Echo"> </WSDL: Part>
</WSDL: Message>

C. Port of the WSDL release

Modify the format to http: // <server URL >:< server port>/<Project Name>

<WSDL: Service name = "helloservice">
<WSDL: Port name = "helloport" binding = "TNS: helloservicesoapbinding">
<Soap12: address location = "http: // localhost: 9090/Hello"/>
</WSDL: Port>
</WSDL: Service>

 

<WSDL: Service name = "helloservice">
<WSDL: Port name = "helloport" binding = "TNS: helloservicesoapbinding">
<Soap12: address location = "http: // localhost: 8088/javafirst"/>
</WSDL: Port>
</WSDL: Service>

6. Delete all spam SRC.

7. Right-click the WSDL file and generate a new interface and implementation code through JAX-WS tools-> generate code.

The client and server are used to test the WebService without the container and are generally selected.

The last generated file list:

8. Edit the helloimpl. Java File

Simple return value:

Public class helloimpl implements hello ...{

Private Static final logger log = logger. getlogger (helloimpl. Class. getname ());

/** // * (Non-javadoc)
* @ See COM. javafirst. PK. Hello # echo (Java. Lang. String MSG )*
*/
Public java. Lang. String echo (Java. Lang. String MSG )...{
Log.info ("executing Operation echo ");
System. Out. println (MSG );
Try ...{
Java. Lang. String _ return = MSG;
Return _ return;
} Catch (exception ex )...{
Ex. printstacktrace ();
Throw new runtimeexception (Ex );
}
}

}

9. Release

Right-click the WSDL file and choose build package... from the menu to generate the war package.

Note that this war package does not contain any library files, so you need to manually package (such as using WinRAR) and add the lib directory to the WEB-INF directory in the war package.

Publish the war package to the application server.

In this case, the published WSDL port is http: // <server >:< port>/<context root>/services/<Project Name>

Here, context root is the context root when the war package is published in was. By default, it is the project name in Tomcat.

Therefore, you can modify the address and port in the WSDL file. Note that you have modified the address here. If the generated client is used for testing, the corresponding address in the client should also be consistent with the address here.

<WSDL: Service name = "helloservice">
<WSDL: Port name = "helloport" binding = "TNS: helloservicesoapbinding">
<Soap12: address location = "http: /localhost: 8088/Hello/services/Hello"/>
</WSDL: Port>
</WSDL: Service>

Then Package the package again and publish it once.

So far, a simple webserive has been released successfully.

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.