Using Apache CXF to develop WebServices server, client

Source: Internet
Author: User
Tags soap wsdl

In my previous blog post, I used xfire1.x to develop the webservies server.

But if you visit Apache's official website, you can see that Xfire has been merged.

The newest frame is called CXF.
Apache CXF = Celtix + XFire.
CXF inherited the essence of the two big open source projects of Celtix and XFire,
Provides full support for JAX-WS and provides multiple Binding, DataBinding, Transport, and various Format support, with code first or WSDL precedence based on actual project needs (WSDL First) to make it easy to publish and use Web Services.

In short, is cxf is good. So then we'll use CXF to develop a webservices server. Experience the benefits of CXF.

-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-

Service Side

-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-

Environment: Myeclipse6.01+jdk5

1) First, build a Web project.

2) write an interface and service class.

Service Interface Ihelloservice.java

Package com.pengzj.service;

Import java.util.List;

Import Javax.jws.WebMethod;
Import Javax.jws.WebService;

/**
*
* @author ATIXUJIE#GZ-ACCP
*
*/
@WebService
Public interface Ihelloservice {

@WebMethod
public string Sayhi (string uname);
}

WebServices implementation class Ihelloserviceimpl. java

Import Com.pengzj.service.IHelloService;

@WebService
public class Ihelloserviceimpl implements ihelloservice{

@Override
@WebMethod
public string Sayhi (string uname) {
Return "Hello" +uname;
}

}

As you can see, here's a webservices note. @WebService and @webmethod.

3) Import the CXF jar package.

Download the CXF package to Apache's website.

The latest version is 2.2.9. Of course, if you download this package it will be more troublesome. Because it needs the latest JDK (1.6.01). Support for what 1.6.U11).

So it is recommended that you download the 2.0.4 address as follows:

Http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip

After decompression, the jar packages under the Lib folder are copied to the project.

4) Configure CXF.

Then change the config file for Web. XML to read as follows:

<display-name>cxf</display-name>
    < Description>cxf</description>
    <servlet>
         <description>apache CXF endpoint</description>
         <servlet-name>cxf</servlet-name>
        < Servlet-class>
            Org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class >
        <load-on-startup>1</load-on-startup>
     </servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>

<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

5) Configure the spring configuration file.

Because the CXF integrates spring. So the above configuration to default to web-inf/to find spring configuration file applicationcontext.xml.

So we're going to create a spring configuration file under Web-inf applicationcontext.xml the following:

<?xml version= "1.0" encoding= "UTF-8"?>
<beans
Xmlns= "Http://www.springframework.org/schema/beans "
Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
Xmlns:jaxws= "Http://cxf.apache.org/jaxws "
Xsi:schemalocation= "Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd " >
<import resource= "Classpath:meta-inf/cxf/cxf.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
<!--Configure WebServices class name and service name--
<jaxws:endpoint id= "Hellows"
Implementor= "Com.pengzj.service.impl.IHelloServiceImpl" address= "/hellows"/>

</beans>

The preparatory work is over.

Deploy, run. Enter at address: http://localhost:8080/cxfws_0619/services/

Should be able to see a hyperlink, click to see the following Wsdl-xml file

<?xml version= "1.0" encoding= "Utf-8"?>

+ <wsdl:definitions xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/" xmlns:ns1= "http// service.pengzj.com/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="/http/ impl.service.pengzj.com/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"Name= " Ihelloserviceimplservice"targetnamespace="http://impl.service.pengzj.com/">

OK. Use the CXF development service end.

Summary steps:

1) Build a Web project. Import the CXF jar package.

2) Configure the Web. xml file

3) Configure the spring configuration file. Configure the bean for the service class at the same time.

4) Deployment run.

-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-

Guest User-side

-=+=--=+=--=+=--=+=--=+=--=+=--=+=--=+=-

The subsequent invocation of the client can theoretically be invoked in any way. Because WebServices's philosophy is

The server and client can be heterogeneous platforms.

In the previous blog, use Xfire as the client. Now we develop the client using the form of automatic generation of the pile code based on the WSDL file.

After extracting the CXF package, there is a bin directory. There is a wsdl2java.bat file. This file can help us build the stub of the client based on the WSDL file. That is, the pile code. The main is to generate and server-side consistent interface files.

Use this command in DOS (actually myclipse has this plugin)

wsdl2java-d d:\src-client http://localhost:8080/cxfService_0617/services/Hellows?wsdl

Some packages and Java files will be generated under the SRC folder of the D drive. src folder you want to build well beforehand.

The file with the longest filename is the main application.

Args[0] represents the location of the WSDL file, you need to modify it manually.

Public final class Ihelloservice_ihelloserviceimplport_client {

private static final QName service_name = new QName ("http://impl.service.pengzj.com/", " Ihelloserviceimplservice ");

Private Ihelloservice_ihelloserviceimplport_client () {
}

public static void Main (String args[]) throws Exception {

String url= "http://localhost:8080/cxfService_0617/services/Hellows?wsdl ";
URL wsdlurl = null;
File Wsdlfile = new file (URL);
try {
if (wsdlfile.exists ()) {
Wsdlurl = Wsdlfile.tourl ();
} else {
wsdlurl = new URL (URL);
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
}
Ihelloserviceimplservice ss = new Ihelloserviceimplservice (Wsdlurl, service_name);
Ihelloservice port = Ss.getihelloserviceimplport ();
{
System.out.println ("Invoking sayhi ...");
Java.lang.String _sayhi_arg0 = "Jack";
Java.lang.String _sayhi__return = Port.sayhi (_sayhi_arg0);
System.out.println ("sayhi.result=" + _sayhi__return);

}

System.exit (0);
}

}

The result of running the above program is:

Invoking Sayhi ...


Sayhi.result=hello Jack.

-----------------

Congratulations, you've succeeded.

Follow up: Because our WebServices method on the server is relatively simple, the string is passed, and the return value is also a string.

If the value passed in or returned has a custom data type. Then the generated client code is more complex.

However, for webservices services, it is recommended to transfer the general data format as well, if the complex data format is passed, then consider improving the design.

Using Apache CXF to develop WebServices server, client

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.