Accueil > Apache CXF, JAX-WS > JAX-WS with Apache CXF and Eclipse [step1]

JAX-WS with Apache CXF and Eclipse [step1]


Few months ago, I had to migrate WebServices from my professional project based on Axis to JAX-WS which is the Java API for XML Web Services supported by the Java Platform, Standard Edition 6 (Java SE 6).

There are several implementations of JAX-WS like :

In my case, my professional project doesn’t use Spring. We have chosen Apache CXF which use Spring by default but you can manage CXF without Spring.

I have discovered and learned CXF with Eclipse JEE Indigo (works too with Eclipse JEE Helios) which provides Wizard to generate JAX-WS annotation and initialize CXF application. I think that playing with the CXF Eclipse Wizard is a good start point to learn CXF, so I decided to write some articles called JAX-WS with Apache CXF and Eclipse (this link is the plan of articles) which explains step by step how to generate WebService and Consumer of WebService with CXF Eclipse Wizard.

In my articles I will use :

  • the last version 2.4.2 of CXF.
  • Tomcat 7 as server.
  • Eclipse JEE Indigo.

But you can do the same thing with another version of CXF and another server. In this article I will explain how to

  1. initialize the CXF Plugin
  2. and create an empty Eclipse Dynamic Web Project with Tomcat 7.

Download

You can download jaxwswithcxf_step1.zip which is a zip which contains the empty Dynamic Web Project jaxwswithcxf.

Why CXF?

My experimentation with Axis2

When I started to play with JAX-WS, I decided to test Axis2 because I was happy with Axis but I was disappointed with the JAX-WS implementation of Axis2 for serveral reasons :

  • documentation is very complex when you are a newbie.
  • axis2 distribution provides a lot of JARs and we don’t know which JARs we must select
  • default deploy service force you to set your WebService JARs in a servicejars folder.
  • that’s why I tried to implement my own deployer to follow my custom architecure (WebService JARs are stored on another folders) but default axis2 deployer code is not based on JAX-WS Endpoint#publish(String address, Object implementor) to deploy a WebService.
  • webservice is not a singleton
  • have some bugs with method which don’t have any parameter, etc.

My experimentation with CXF

After my Axis2 experimentation, I have noticed that Eclipse JEE Indigo provides a wizard to generate JAX-WS annotation and initialize a Dynamic Web Project with CXF by using Spring. Unfortunately we have not Spring in our project, but CXF can work without Spring! I will explain to you how to do that in the next articles.

Today I’m not disappointed in CXF for several reasons :

  • Eclipse JEE Indigo generate well JJAX-WS annotation for the Java class you wish publish as WebService.
  • If you can use Spring, the CXF Wizard is very helpfull because it generate you the well web.xml with Spring CXF Servlet and Spring beans which are used to declare classes which must be published as WebService.

    Writing a service with Spring explains that.

  • If you don’t use Spring, you can still use the CXF Wizard to generate JAX-WS annotation. For the publish of the WebService you need to do that manually with Java code by using JAX-WS API Endpoint#publish(String address, Object implementor) to deploy a WebService :
    MyService serviceInstance = new MyService();
    Endpoint.publish("http://localhost:8080/MyService", serviceInstance);
    

    will deploy your service available at the http://localhost:8080/MyService.

    A simple JAX-WS service explains that.

  • CXF architecture is based on CXF Interceptors (In/Out) to do some process before (In)/after (Out) calling your WebService. You can use existing CXF Interceptor or develop your own Interceptor to manage logging, security, etc.
  • CXF Team is very available. As soon as you post your questions/requirements on the CXF Mailing list, you have an answer.

Install CXF

CXF distribution provides several tools to generate JAX-WS annotation, WSDL, etc. The CXF Eclipse Plugin use those tools for the CXF wizard.

Installing CXF means :

Download CXF distribution

Go to the CXF Download and download the last version of the CXF distribution. In this article apache-cxf-2.4.2.zip is used :

Unzip apache-cxf-2.4.2.zip on your Hard Disk wherever you want. In my case I have unzipped to C:\Apache\apache-cxf-2.4.2 :

Initialize CXF Eclipse Plugin

In this section we will set the path of the CXF distribution in the CXF Eclipse Preferences. Open your Eclipse JEE (Helios or Indigo) and go to the Window / Preferences menu :

This action opens the Preferences dialog. Click on the Web Services / CXF 2.x Preferences menu item :

Click on Add… button (on the right of the dialog) to select the path of the CXF distribution. This action opens the Add CXF Runtime. Add Browse… button and select the path of CXF distribution (in my case C:\Apache\apache-cxf-2.4.2) :

If the path of CXF distribution is valid, the fields of the dialog are filled automatically like this :

Click on Finish button, the dialog closes and CXF Runtime tab is filled with the CXF Runtime :

At this step, CXF Runtime is not available. Click on checkbox to make it available :

CXF Runtime is now available. In the next article we will see that CXF Runtime will be present in the generic WebServices wizard.

Initialize CXF Web Application

Here we will create an empty Dynamic Web Project named jaxwswithcxf which will host the JAX-WS WebService.

Download Tomcat 7

Go to the Tomcat 7 Download and download the last version of the Tomcat distribution. In this article apache-tomcat-7.0.20.zip is used :

Unzip apache-tomcat-7.0.20.zip on your Hard Disk wherever you want. In my case I have unzipped to C:\Apache\apache-tomcat-7.0.20 :

Create Dynamic Web Project

In this section we will create an empty jaxwswithcxf WEB application with Dynamic Web Project. To do that go to menu File/New /Dynamic Web Project :

New Dynamic Web Project wizard opens. Fill in the project name with jaxwswithcxf and select Target runtime with your Server. If no server is defined, click on New Runtime… button to define it.

Define New Server

Click on New Runtime… button, open the wizard to create a server. Select Tomcat v7.0 Server :

Click on Next button, the wizard page displays the selection of the Tomcat directory (existing Tomcat or empty folder where Tomcat must be installed). Here we will select the installed Tomcat 7. Click on the Browse… button to select the Tomcat home (in my case C:\Apache\apache-tomcat-7.0.20) :

New Dynamic Web Project configurated

Click on the Finish button, the New Dynamic Web Project wizard page appears :

Fields of this wizard are pre-filled like 3.0 Dynamic Module Web version which means that Tomcat 7.0 support 3.0 servlet (you can select another version if you wish). Click on Finish Button, the empty jaxwswithcxf WEB application is generated :

Launch Empty WEB application

Now you can start the Tomcat server and launch the empty WEB Application. Select the jaxwswithcxf project, click on right mouse button and select menu Run As->Run on Server

This action opens the wizard to select the server :

Select Tomcat 7.0 server and click on the Finish button. This action create a Server folder in your workspace which is the configuration of your Tomcat and launch the jaxwscxf WEB Application :

You can notice that you will have a 404 error because the generated web.xml is like this :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>jaxwswithcxf</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

And your web application contains no such files.

Conclusion

In this article we have initialized CXF Eclipse Plugin and created an empty Dynamic Web Project with Tomcat 7.

In the next article [step2] we will create a simple Java class HelloServiceImpl and we will use CXF Eclipse wizard to expose it as WebService with JAX-WS and CXF.

Catégories :Apache CXF, JAX-WS
  1. novembre 28, 2011 à 10:25

    Hi, Angelo!

    I’m the web editor at iMasters, one of the largest developer communities in Brazil. I´d like to talk to you about republishing your article at our site.

    Can you contact me at rina.noronha@imasters.com.br?

    Bests,
    Rina Noronha
    Journalist – web editor
    http://www.imasters.com.br
    redacao@imasters.com.br
    rina.noronha@imasters.com.br
    +55 27 3327-0320 / +55 27 9973-0700

  2. Eric Givler
    décembre 23, 2011 à 2:34

    two things:
    1. Is there a reason not to use the Windows Installer version of Tomcat?
    2. When I did this example, it neve created a web.xml file in my project? I couldn’t find anything under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps, that showed jaxwscxf as a web project.

    there’s a stracktrace in Eclipse under the Console window for Tomcat that shows:

    Dec 23, 2011 9:31:37 AM org.apache.catalina.startup.ContextConfig checkHandlesTypes
    WARNING: Unable to load class [org.apache.xml.resolver.tools.ResolvingXMLReader] to check against the @HandlesTypes annotation of one or more ServletContentInitializers.
    java.lang.ClassNotFoundException: org.apache.xml.resolver.tools.ResolvingXMLReader
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:1947)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:1910)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1797)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1756)
    at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1742)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1245)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:874)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:317)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4974)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1035)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1035)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:724)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:620)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:304)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)

    • décembre 23, 2011 à 3:07

      >1. Is there a reason not to use the Windows Installer version of Tomcat?
      No but Web server installation (like Tomcat) is not very interesting for this article focused on CXF JAX-WS .

      >2. When I did this example, it neve created a web.xml file in my project? I couldn’t find anything under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps, that showed jaxwscxf as a web project.
      >there’s a stracktrace in Eclipse under the Console window for Tomcat that shows:

      Sorry I cannot help you with that (it’s not really CXF JAX-WS question, but a Tomcat installation problem).

      Good luck.

      Regards Angelo

  3. Santiago
    avril 20, 2012 à 8:42

    The web.xml file is no longer created by default. You have to check a checkbox in the Dynamic Web Project wizard. It’s on the last page (Web Module) of the wizard : « Generate web.xml deployment descriptor ».

  4. dgf
    décembre 24, 2012 à 1:56

    Thanks Angelo, thanks.

    I’ve spent 3 hours trying to set up CXF for eclipse and most of the sites trying to explain it fails. I’m aiming to make a Web Service running at AWS Elastic Beanstalk so I hope to achieve it with your tutorial and this one:

    http://www.soa.si/2012/11/23/using-jax-ws-and-aws-elastic-beanstalk-to-design-develop-and-deploy-java-web-services/

    Thanks!

  5. chandra
    avril 2, 2013 à 11:26

    Excellent guide.. I am able to create webservice now with CXF on the fly..

  6. Anshul
    mars 6, 2014 à 11:32

    My environment is :
    Eclipse helios
    WAS 8.0
    JDK 1.6
    CXF 2.7.10

    I have congigured the CXF in eclipse as per step1 and I try to create a CXF Web Service from a wsdl (Top to Down ). I am getting the below error. Can you please help me to fix this issue?

    org.apache.cxf.tools.wsdlto.core.PluginLoader init
    SEVERE: Tools plugin provider jaxb context init failed
    Exception in thread « main » org.apache.cxf.tools.common.ToolException: Tools plugin provider jaxb context init failed
    at org.apache.cxf.tools.wsdlto.core.PluginLoader.init(PluginLoader.java:81)
    at org.apache.cxf.tools.wsdlto.core.PluginLoader.(PluginLoader.java:70)
    at org.apache.cxf.tools.wsdlto.core.PluginLoader.newInstance(PluginLoader.java:104)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.(WSDLToJava.java:48)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.main(WSDLToJava.java:181)

  7. Mai 6, 2014 à 12:08

    Perfect tutorial…..

  8. javi
    juillet 4, 2015 à 12:08

    Thanks very much! It was very useful for me. Congratulations.

  1. août 23, 2011 à 3:31
  2. août 24, 2011 à 5:40
  3. août 24, 2011 à 3:59
  4. septembre 13, 2016 à 5:28

Répondre à Santiago Annuler la réponse.