Categories

CXF+SPRING+POJO

Full maven project can be downloaded from HERE.

This is a follow up article to XFIRE+EJB+POJO.
Here I show how to expose POJO using CXF project instead of XFire. Setting up WebServices with CXF is very easy and consists of two steps:

  • Setting up web.xml with CXFServlet.
  • Defining POJO endpoint.


  • Setting up web.xml with CXFServlet.

    <!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></display-name> <!-- CXFServlet expect these spring configs already loaded by listeners, endpoint context files will be defined as servlet param. This will allow us to have different CXFServlets with different endpoints --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/META-INF/cxf/cxf.xml classpath:/META-INF/cxf/cxf-servlet.xml classpath:/META-INF/cxf/cxf-extension-soap.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>numberservice</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <param-name>config-location</param-name> <param-value>/WEB-INF/numberservice-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>numberservice</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>

    Most of CXF examples have one spring context file that gets loaded through a servlet listener.
    The one above shows how to have multiple CXFServlets defined with their own endpoints context file.

    Defining POJO endpoint.

    <?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:simple="http://cxf.apache.org/simple" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">     <!-- ENDPOINTS --> <bean name="numberService" class="com.tsolak.numberservice.NumberServiceImpl"> </bean>     <simple:server id="numberServiceEndpoint" address="/numberservice" serviceClass="com.tsolak.numberservice.NumberService" endpointName="e:NumberService" serviceName="s:NumberService" xmlns:e="http://service.jaxws.cxf.apache.org/endpoint" xmlns:s="http://numberservice.tsolak.com"> <simple:serviceBean> <ref bean="numberService"/> </simple:serviceBean> </simple:server> </beans>

    Like in XFire with EJB article “numberService” bean can be replaced with EJB proxy in order to expose EJB as SOAP WebService.

    After deploying to Tomcat hit http://localhost:8080/number-services-webapp/numberservice?wsdl for autogenerated WSDL.

5 comments to CXF+SPRING+POJO

  • Olivier

    Hi,

    These two articles dealing with exposing services as web services are wonderful. Deployments work like a charm !

    I’m still fighting with exposing the EJB from the previous article with CXF. I’m getting a weird NPE when deploying to weblogic.

    The only thing I did in the spring config file is replacing

    with

    Anything you’d advise to do ?

    Thanks again.

    Exception:
    java.lang.NullPointerException
    at org.apache.cxf.interceptor.AnnotationInterceptors.getAnnotationObject(AnnotationIntercept
    ors.java:50)

  • Olivier

    Missing the xml snippet. I replace numberService with NumberServiceSessionBeanProxy in the simple:serviceBean definition

  • tsolakp

    Did it work for you? I have only tested this project on Tomcat.

  • Isaias

    Could you explain a little what is being specified in the POJO Endpoint definition?

  • tsolakp

    “simple:server” creates adapter similar to “XFireExporter” (as explained in XFIRE+EJB+POJO article) which listens to http request at “base servler path/numberservice” address and converts SOA request to POJO method call and return value to XML according to its autogenerated WSDL.
    The “endpointName=:NumberService”, “serviceName=:NumberService”, “xmlns:e=http://service.jaxws.cxf.apache.org/endpoint
    ” and “xmlns:s=http://numberservice.tsolak.com” are used in defining WSDL namespaces.
    For more check http://cwiki.apache.org/CXF20DOC/simple-frontend.html and http://cwiki.apache.org/CXF20DOC/writing-a-service-with-spring.html.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>