Web Services Hello World Example with CXF

CXF Overview

Apache CXF is an open source, fully featured Web services framework, its name CXF is originated from Celtix and XFire, the two projects are nicely combined work together to Apache. CXF supports JAX-WS, Binding, DataBinding and Transport implementation, the nice feature is its embeddable Web service component: (e.g. integrated Spring Framework and Geronimo), CXF has been designed to provide a pluggable architecture that supports not only XML but also non-XML type bindings, such as JSON and CORBA, in combination with any type of transport. in this how-to, we look though a step-by-step hello world example with CXF to start the first web service project.

Prerequisites

please hava JDK installed and download the CXF latest release version from apache website.

1. Create a project

Use IDE(Eclipse/Idea), Create a new Java project and set below dependency libraries as classpath, Unpack the CXF download distribution file, add the following mandatory jars to dependency libraries of project in order to use full CXF functionality.

1. commons-logging-1.1.1.jar2. geronimo-activation_1.1_spec-1.0.2.jar (or Sun’s Activation jar)3. geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
4. geronimo-javamail_1.4_spec-1.6.jar (or Sun’s JavaMail jar)
5. geronimo-servlet_2.5_spec-1.2.jar (or Sun’s Servlet jar)
6. geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
7. geronimo-jaxws_2.1_spec-1.0.jar (or Sun’s jaxws-api-2.1.jar)
8. geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
9. jaxb-api-2.1.jar
10. jaxb-impl-2.1.12.jar
11. jetty-6.1.21.jar
12. jetty-util-6.1.21.jar
13. neethi-2.0.4.jar
14. saaj-api-1.3.jar
15. saaj-impl-1.3.2.jar
16. wsdl4j-1.6.2.jar
17. wstx-asl-3.2.8.jar
18. XmlSchema-1.4.5.jar
19. xml-resolver-1.2.jar
20. cxf-2.2.2.jar

2. Create a web service interface “IHelloWorld” with one method sayHi.

IHelloWorld.java
@WebService
public interface IHelloWorld {
   //@WebParam is optional
  String sayHi(@WebParam(name="text") String text);
}

3. Implement this webservice interface with Annotation @WebService.

HelloWorldImpl.java
@WebService
public class HelloWorldImpl implements IHelloWorld {
  public String sayHi(String name) {
    System.out.println("sayHello is called by " + name);
    return "Hello " + name;
  }
}

4. Create a webservice server

Bean JaxWsServerFactoryBean is to help easily create Server endpoints for JAX-WS, we instantiate JaxWsServerFactoryBean and set service class, service bean and address, method create() will start a server and register it with the ServerManager.

Server.java
public class Server {
  private Server() {
    IHelloWorld helloWorld = new HelloWorldImpl();
    //create WebService service factory
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    //register WebService interface
    factory.setServiceClass(IHelloWorld.class);
    //publish the interface
    factory.setAddress("http://localhost:9000/HelloWorld");
    factory.setServiceBean(helloWorld);
    //create WebService instance
    factory.create();
  }

  public static void main(String[] args) throws InterruptedException {
    //now start the webservice server
    new Server();
    System.out.println("Server ready...");
    Thread.sleep(1000 * 60);
    System.out.println("Server exit...");
    System.exit(0);
  } 
}

in IDE or use Command Line, runing the static method main to start this web service, once it is started, type URL http://localhost:9000/HelloWorld?wsdl in address bar of browser, the WSDL should be displayed as below.

<wsdl:definitions name="IHelloWorldService" targetNamespace="https://asjava.com/">
<ul>
	<li><wsdl:types></li>
	<li><xsd:schema attributeFormDefault="unqualified" 
elementFormDefault="unqualified" targetNamespace="https://asjava.com/"></li>
	<li><xsd:element name="sayHi" type="tns:sayHi"/></li>
	<li><xsd:complexType name="sayHi"></li>
	<li><xsd:sequence></li>
	<li><xsd:element minOccurs="0" name="text" type="xsd:string"/></li>
	<li></xsd:sequence></li>
	<li></xsd:complexType></li>
	<li><xsd:element name="sayHiResponse" type="tns:sayHiResponse"/></li>
	<li><xsd:complexType name="sayHiResponse"></li>
	<li><xsd:sequence></li>
	<li><xsd:element minOccurs="0" name="return" type="xsd:string"/></li>
	<li></xsd:sequence></li>
	<li></xsd:complexType> ....</li>
</ul>

5. Create a client to verify whether the web service server works.

The Web service server has been published, now we are going to write a piece of client program to call this service.

Client.java
public class Client {
  private Client() {
  }

  public static void main(String[] args) {
    //create WebService client proxy factory
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    //register WebService interface
    factory.setServiceClass(HelloWorld.class);
    //set webservice publish address to factory.
    factory.setAddress("http://localhost:9000/HelloWorld");
    IHelloWorld iHelloWorld = (IHelloWorld) factory.create();
    System.out.println("invoke webservice...");
    System.out.println("message context is:" + iHelloWorld.sayHi("Josen"));
    System.exit(0);
  }
}

We can run the client main method to remotely call the web service, after that, switch to console of web service server, should be able to see that a message sayHello is called by Josen was printed.

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

*

code

  1. Roger

    Great article, When I run the example I got the error java.net.BindException: Address already in use: JVM_Bind , can you help me how to resolve it? I am really new of java

    Ответить
    1. admin автор

      This error means your port is taking place by other program, please use a different port.

      Ответить
  2. CHANDAN AGARWAL

    I am a complete begineer to WSS and CXF. I tried your example and a few doubts hav come to my mind.
    a) could you please explain where to create the wsdl file
    b) your wsdl file is incomplete, could you give the entire code for that
    thanks

    Ответить
    1. Jammy

      I am sorry late reply.
      a). Those wsdl files were created by myself, just according by your needs and the rule of WSDL to code it. it’s very simple and both methods have same parts, also, some tools can create WSDL, please Google it.
      b). Yes, my WSDL is in-completed, because the whole WSDL is very long so that I just put the main part on this, if you need just tell me I can email you.

      Ответить
  3. admin автор

    That’s fine, Hopefully this article “CXF Web Services hello world example” is useful for you anyway!

    Ответить
  4. Ofir

    Hi
    I am trying to create my web service on teh jetty (using ant)

    but my wsdl is not created or at least I cant find it

    Since your post was very good (licking never harmed) can you please instruct me on how to create the wsdl
    I have tried googling it, but whatever i find wasnt helpfull for me
    Warm regards
    Ofir

    Ответить
  5. molo

    The sample looks pretty straightforward.
    It’s complete project. It means Java project with 4 java files only. Note – use the same set of 20 jars (don’t copy all from apache-cxf-2.x\lib folder).

    Looking forward for CXF sample using Tomcat and Ant.
    Thanks!

    Ответить
    1. admin автор

      Yes, you are right. I will put another example with jetty in short time.

      Ответить
  6. Jean Villete

    Hey guy.
    I would like to thank you so much.
    I know, there’s a long time ago you did this post, but so far still helping a lot of people, like me.

    Thanks again.

    Ответить
  7. whiteWoolve

    Hi,

    this overview is a good intro to cxf. I would appreciate a more practical example but this is enough to get a taste of cxf. I think you are leaning on Apache CXF 2.6.6 in this article

    *TY*

    Ответить
    1. admin автор

      Yes, when I wrote this article, the latest CXF version was 2.6.6. I believe they had release several new versions, but whatever, the guide is same, but just use the different jars.

      Ответить
  8. Gaurav Arya

    Hi, I found your post quite useful. However, I would be thankful to you if you could show the contract first approach using cxf and eclipse.

    Thanks
    Gaurav Arya

    Ответить