Date Created: Mon 18-Oct-2010

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/



    Apache CXF Example - Part 2

    " projects from Eclipse Galileo

    Creating my first CXF project - Part 2

    the goal here is to take the sample server code and make into a WAR file using Ecilpse dynamic web project.

    I renamed (Refactored) the client project to CXFClient and copied to CXFServer. The CXFClient project can be made into a standard Java application project as it was created a dynamic web project, yet we didn't want to build a WAR.

    I am now using the CXFServer web project.

    Moved logging.properties to WEB-INF




    What I then did was look at the build.xml and common_build,xml for the build target and trace the targets to see how the build process prepares the required WAR layout if the WAR target is built.

    We have not called WAR yet, so I am going back to the sample to call the WAR target and see the resulting WAR, this is quicker than trying to read the ant and dedude my target tracing.


    <target name="war" depends="build">
    <cxfwar filename="${cxf.war.file.name}.war"/>
    </target>

    Command line



    ant war

    same as ant -f build.xml war



    I just love ANT so much, this guy was clever. He invented ANT as he wrote Tomcat.





    To allow the use of the jar command we need to add C:\Java\jdk1.6.0_20\bin to the path

    C:\apache-cxf-2.2.7\samples\java_first_jaxws\build\war>set path=%path%;C:\Java\jdk1.6.0_20\bin

    we should set in the sytem properties of windows for persistence of the path.

    lets expand the WAR file built by the ant command, using the JAR command. This is great for when on unix systems.

    C:\apache-cxf-2.2.7\samples\java_first_jaxws\build\war>jar -xvf helloworld.war



    Here is the resulting file structure



    Lets create a test file of the WAR structure

    C:\apache-cxf-2.2.7\samples\java_first_jaxws\build\war>jar -xvf helloworld.war > structure.txt

    Result of text file to analysis

    created: META-INF/
    inflated: META-INF/MANIFEST.MF
    created: WEB-INF/
    inflated: WEB-INF/web.xml
    created: WEB-INF/classes/
    created: WEB-INF/classes/demo/
    created: WEB-INF/classes/demo/hw/
    created: WEB-INF/classes/demo/hw/client/
    created: WEB-INF/classes/demo/hw/server/
    inflated: WEB-INF/classes/demo/hw/client/Client.class
    inflated: WEB-INF/classes/demo/hw/server/HelloWorld.class
    inflated: WEB-INF/classes/demo/hw/server/HelloWorldImpl.class
    inflated: WEB-INF/classes/demo/hw/server/IntegerUserMap$IntegerUserEntry.class
    inflated: WEB-INF/classes/demo/hw/server/IntegerUserMap.class
    inflated: WEB-INF/classes/demo/hw/server/IntegerUserMapAdapter.class
    inflated: WEB-INF/classes/demo/hw/server/Server.class
    inflated: WEB-INF/classes/demo/hw/server/User.class
    inflated: WEB-INF/classes/demo/hw/server/UserAdapter.class
    inflated: WEB-INF/classes/demo/hw/server/UserImpl.class
    inflated: WEB-INF/cxf-servlet.xml
    created: WEB-INF/wsdl/
    inflated: WEB-INF/wsdl/cxf-servlet.xml
    created: WEB-INF/lib/
    inflated: WEB-INF/lib/FastInfoset-1.2.7.jar
    inflated: WEB-INF/lib/XmlSchema-1.4.5.jar
    inflated: WEB-INF/lib/abdera-core-0.4.0-incubating.jar
    inflated: WEB-INF/lib/abdera-extensions-json-0.4.0-incubating.jar
    inflated: WEB-INF/lib/abdera-extensions-main-0.4.0-incubating.jar
    inflated: WEB-INF/lib/abdera-i18n-0.4.0-incubating.jar
    inflated: WEB-INF/lib/abdera-parser-0.4.0-incubating.jar
    inflated: WEB-INF/lib/ant-1.6.5.jar
    inflated: WEB-INF/lib/antlr-2.7.7.jar
    inflated: WEB-INF/lib/aopalliance-1.0.jar
    inflated: WEB-INF/lib/asm-2.2.3.jar
    inflated: WEB-INF/lib/axiom-api-1.2.7.jar
    inflated: WEB-INF/lib/axiom-impl-1.2.7.jar
    inflated: WEB-INF/lib/bcprov-jdk15-1.43.jar
    inflated: WEB-INF/lib/commons-codec-1.3.jar
    inflated: WEB-INF/lib/commons-collections-3.2.1.jar
    inflated: WEB-INF/lib/commons-lang-2.4.jar
    inflated: WEB-INF/lib/commons-logging-1.1.1.jar
    inflated: WEB-INF/lib/commons-pool-1.5.2.jar
    inflated: WEB-INF/lib/cxf-2.2.7.jar
    inflated: WEB-INF/lib/cxf-manifest.jar
    inflated: WEB-INF/lib/cxf-xjc-boolean-2.2.7.jar
    inflated: WEB-INF/lib/cxf-xjc-bug671-2.2.7.jar
    inflated: WEB-INF/lib/cxf-xjc-dv-2.2.7.jar
    inflated: WEB-INF/lib/cxf-xjc-ts-2.2.7.jar
    inflated: WEB-INF/lib/geronimo-activation_1.1_spec-1.0.2.jar
    inflated: WEB-INF/lib/geronimo-annotation_1.0_spec-1.1.1.jar
    inflated: WEB-INF/lib/geronimo-javamail_1.4_spec-1.6.jar
    inflated: WEB-INF/lib/geronimo-jaxws_2.1_spec-1.0.jar
    inflated: WEB-INF/lib/geronimo-jms_1.1_spec-1.1.1.jar
    inflated: WEB-INF/lib/geronimo-stax-api_1.0_spec-1.0.1.jar
    inflated: WEB-INF/lib/geronimo-ws-metadata_2.0_spec-1.1.2.jar
    inflated: WEB-INF/lib/jaxb-api-2.1.jar
    inflated: WEB-INF/lib/jaxb-impl-2.1.12.jar
    inflated: WEB-INF/lib/jaxb-xjc-2.1.12.jar
    inflated: WEB-INF/lib/jaxen-1.1.jar
    inflated: WEB-INF/lib/jettison-1.2.jar
    inflated: WEB-INF/lib/jra-1.0-alpha-4.jar
    inflated: WEB-INF/lib/js-1.7R1.jar
    inflated: WEB-INF/lib/jsr311-api-1.0.jar
    inflated: WEB-INF/lib/neethi-2.0.4.jar
    inflated: WEB-INF/lib/oro-2.0.8.jar
    inflated: WEB-INF/lib/saaj-api-1.3.jar
    inflated: WEB-INF/lib/saaj-impl-1.3.2.jar
    inflated: WEB-INF/lib/serializer-2.7.1.jar
    inflated: WEB-INF/lib/slf4j-api-1.5.8.jar
    inflated: WEB-INF/lib/slf4j-jdk14-1.5.8.jar
    inflated: WEB-INF/lib/spring-beans-2.5.6.jar
    inflated: WEB-INF/lib/spring-context-2.5.6.jar
    inflated: WEB-INF/lib/spring-context-support-2.5.6.jar
    inflated: WEB-INF/lib/spring-core-2.5.6.jar
    inflated: WEB-INF/lib/spring-jms-2.5.6.jar
    inflated: WEB-INF/lib/spring-tx-2.5.6.jar
    inflated: WEB-INF/lib/spring-web-2.5.6.jar
    inflated: WEB-INF/lib/velocity-1.6.2.jar
    inflated: WEB-INF/lib/wsdl4j-1.6.2.jar
    inflated: WEB-INF/lib/wss4j-1.5.8.jar
    inflated: WEB-INF/lib/wstx-asl-3.2.9.jar
    inflated: WEB-INF/lib/xalan-2.7.1.jar
    inflated: WEB-INF/lib/xml-resolver-1.2.jar
    inflated: WEB-INF/lib/xmlbeans-2.4.0.jar
    inflated: WEB-INF/lib/xmlsec-1.4.3.jar

    All the classes in RED I will not be adding unless required when deployed to container

    OK lets create our WAR

    Delete the client packages




    Lets open the web.xml of the expanded WAR and see what we need and add it to our web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    <display-name>cxf</display-name>
    <description>cxf</description>
    <servlet>
    <servlet-name>cxf</servlet-name>
    <display-name>cxf</display-name>
    <description>Apache CXF Endpoint</description>
    <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>
    </web-app>


    OK, now lets add the web service desriptor cxf-servlet by adding a new xml file



    Select WEB-INF folder



    Result





    contents of cxf-servlet

    <?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"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">

    <jaxws:server id="jaxwsService" serviceClass="demo.hw.server.HelloWorld" address="/hello_world">
    <jaxws:serviceBean>
    <bean class="demo.hw.server.HelloWorldImpl" />
    </jaxws:serviceBean>
    </jaxws:server>
    </beans>


    I guess we will have to add a few jars for this service before it will work, anyway lets continue by importing the appropriate JARS for CXF




    i am only going to add the single JAR cxf-manifest.jar





    I am now going to add the wsdl folder. for some reason the samples WAR web service descriptor is in twice, we can deal with that later?




    That should be all she wrote, lets export the WAR and compare the two








    Compare only shows JAR difference, we will work them out as required.




    Lets depoy to TOM

    Error


    SEVERE: Error loading WebappClassLoader
    delegate: false
    repositories:
    /WEB-INF/classes/
    ----------> Parent Classloader:
    org.apache.catalina.loader.StandardClassLoader@ca2dce
    org.apache.cxf.transport.servlet.CXFServlet
    java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet

    Looked around NET and as I suspected earlier to run web service we need a whole lot of JARS
    http://www.benmccann.com/dev-blog/web-services-tutorial-with-apache-cxf/


    aopalliance-1.0.jar
    commons-logging-1.1.jar
    cxf-2.0-incubator.jar
    geronimo-activation_1.1_spec-1.0-M1.jar (or Sun’s Activation jar)
    geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
    geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun’s JavaMail jar)
    geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun’s Servlet jar)
    geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
    jaxb-api-2.0.jar
    jaxb-impl-2.0.5.jar
    jaxws-api-2.0.jar
    jetty-6.1.5.jar
    jetty-util-6.1.5.jar
    neethi-2.0.jar
    saaj-api-1.3.jar
    saaj-impl-1.3.jar
    spring-core-2.0.4.jar
    spring-beans-2.0.4.jar
    spring-context-2.0.4.jar
    spring-web-2.0.4.jar
    stax-api-1.0.1.jar
    wsdl4j-1.6.1.jar
    wstx-asl-3.2.1.jar
    XmlSchema-1.2.jar
    xml-resolver-1.2.jar

    ================


    Copied in files of the latest XCF from sample lib folder....




    ====================


    SEVERE: StandardWrapper.Throwable
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 2 in XML document from /WEB-INF/cxf-servlet.xml is invalid; nested exception is org.xml.sax.SAXParseException: Premature end of file.




    Doh, forgot paste in the content of the file

    tried to run client code form CXFClient project

    INFO: Creating Service {http://server.hw.demo/}HelloWorld from class demo.hw.server.HelloWorld
    29-Apr-2010 23:26:57 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
    WARNING: Interceptor for {http://server.hw.demo/}HelloWorld#{http://server.hw.demo/}sayHi has thrown exception, unwinding now
    org.apache.cxf.interceptor.Fault: Could not send Message.
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:484)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:310)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:262)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
    at $Proxy44.sayHi(Unknown Source)
    at demo.hw.client.Client.main(Client.java:54)
    Caused by: java.net.ConnectException: ConnectException invoking http://localhost:9000/helloWorld: Connection refused: connect

    then, changed client as follows for TOMCAT.

    String endpointAddress = "http://localhost:8080/CFXServer/helloWorld";

    Caused by: java.io.IOException: HTTP response '404: /CFXServer/helloWorld'
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2187)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2134)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1988)
    ... 11 more





    Changed context Root to CXFServer (note these projects need cleaning and building from scratch as my local tomcat is not showing context root properly?


    Lets load the web service

    http://localhost:8080/CXFServer/services/





    Lets run client and try

    String endpointAddress = "http://localhost:8080/CFXServer/services/hello_world"; //Tomcat



    Success


    Now we need to understand how it all bind together ie how the actual CXF servlet provides the actual service, but we got it working.

    FYI we can load the wsdl, and later to a top down service is generate code form WSDL



    Goals:

    Try to create new projects
    Send and XML Message (XML Document ie Create Journey)
    Understand lifecycle or a Webservice using this API
    Try a Spring Bean approach
    Try a WSDL first approach

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories