CXF Overview
CXF has been taken full advantage of JAX-WS and Spring 2.x XML. In previous chapter, we share Web Services hello world example with CXF, today we look at how to create and publish CXF web service with spring. You will learn the following items:
- Set up how to code Web service with CXF
- Publish a JAX-WS service and client with spring supported
1. Get first step with cxf spring support.
First off, please download Apache CXF, the latest version is 2.2.10, in IDE, create a new project and add libraries to dependencies, note spring libraries is required because we get use later on, the following is a list of required libraries.
2.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.jaxb-api-2.1.jar
9.jaxb-impl-2.1.12.jar
10.neethi-2.0.4.jar
11.saaj-api-1.3.jar
12.saaj-impl-1.3.2.jar
13.wsdl4j-1.6.2.jar
14.wstx-asl-3.2.8.jar
15.XmlSchema-1.4.5.jar
16.xml-resolver-1.2.jar
17.cxf-2.2.10.jar
18.spring-beans.jar
19.pring-context.jar
20.spring-core.jar
21.spring-web.jar
2. Write your CXF Web Service.
Define the web service interface and its implementation. use @WebService
annotate the class.
package com.asjava; @WebService(targetNamespace = "http://asjava.com/webservice ", name = "SimpleService") public interface SimpleService { void sayHello (@WebParam(name="gid") String gid); }
package com.asjava; @javax.jws.WebService(name = "SimpleService", serviceName = "Simple", targetNamespace = "http://asjava.com/webservice", wsdlLocation = "asjava.wsdl", endpointInterface = "com.asjava.SimpleService") public class SimpleServiceImpl implements SimpleService { void sayHello (String gid){ System.out.println(gid); } }
3. Declare web service endpoint in Spring configuration XML.
Configure a JAX-WS server by using jaxws:endpoint
, this element is associated with the org.apache.cxf.jaxws.EndpointImpl
object. The content of spring configuration XML contents is listed below, you can save it with name “beans.xml” for this file.
<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:ipmd="http://avid.com/interplay/ws/assets" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" > <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <bean id="simpleWebService" class="com.asjava.SimpleServiceImpl" /> <jaxws:endpoint id="simpleWebServiceEndpoint" implementor="simpleWebService" wsdllocation="simple.wsdl" address="/simple"> <jaxws:outinterceptors> <bean class="com.asjava.MTOMInterceptor"> </bean> </jaxws:outinterceptors> <jaxws:outfaultinterceptors> <bean class="com.asjava.MTOMInterceptor"> </bean> </jaxws:outfaultinterceptors> </jaxws:endpoint> </beans>
There are attributes of jaxws:endpoint
I’d like to explain:
- id: The unique identifier for a bean.
- Implementor: Specifies the class implementer of the service. either use the class name or an ID reference to a Spring bean configuration. make sure this class is in the classpath.
- wsdlLocation: Specifies the location of the endpoint’s WSDL contract. The location is relative to the folder where the service is deployed, you may read CXF WSDLtoJava if you have WSDL ready.
- address: Specifies the HTTP address of the endpoint. This value will override the value specified in the services contract.
- jaxws:outInterceptors: Specifies a list of interceptors to process outgoing responses. it’s optional.
- jaxws:outFaultInterceptors: Specifies a list of interceptors to process outgoing fault messages.
For CXF Interceptor, we ready have a chapter: http://asjava.com/web-services/cxf-interceptor-read-http-header/
The elements jaxws:outInterceptors and jaxws:outFaultInterceptors
in CXF spring file can be removed since we don’t need use it in this tutorial.
3. Configure web.xml and create a CXFServlet.
In web.xml, we defined a request dispatcher with class org.apache.cxf.transport.servlet.CXFServlet
, <servlet-mapping>
specifies the web container of which java servlet should be invoked for a url given by client. In previous step, we have a beans.xml now we need invoke it in context-param
. now you can do is assemble all files to a war file, copy it to a web container and start it.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>CXF Spring Support Example</description> <display-name>CXF Spring Support Example</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:beans.xml classpath:META-INF/cxf/cxf-servlet.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
4. Create a web service Client in CXF Spring configuration XML.
Like what the <jaxws:endpoint>
is used on the server side, we use spring file to configure jaxws:client
, the parameter id, serviceClass and address need to be set as following example.
<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- Import Apache CXF Bean Definition --> <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" /> <jaxws:client id="SimpleServiceClient" serviceclass="com.asjava.SimpleService" address="http://localhost:80/services/simple"> </jaxws:client> </beans>
Coding below that loads this CXF spring client XML and create web service client.
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"Cxf_Client.xml"}); SimpleService client = (SimpleService) context.getBean("SimpleServiceClient");
It’s one way to create CXF web service client with spring supported, if you have interesting, take read CXF client.