Good tutorials and resources on Apache CXF How Tos are not easy to digg. I had to spend hours searching and reading to make my small application up and running, Integrating Spring with JSF was pretty straightforward, but when it comes to integrating those two with JSF i got stuck.
So this post is about exposing a web service as a web project using JSF front end / Spring backed and CXF for service invocation
before starting I should mention few valuable resource around the net.
- The CXF documentation (Hope it will be completed soon)
- Make Web Services Transparent with Spring 2.5 and Apache CXF 2.0 by Willie Wheeler
- Introduction to Web services creation using CXF and Spring by Rajeev Hathi and Naveen Balani
- Developing JAX-WS Web Service Clients – netbeans
- Sample Application using JAX-WS, JSF, Spring, and Java by Carol Mcdonal
The web service u used was the publicly available spelling checker which is used in the netbeans tutorial.
The Step by step guide as follows >>
Step 1 :
Create the classes from the WSDL you can use netbeans for this task or WSDL2JAVA command (wsdl2java [URL]) in the shell.
Step2 :
Put the generated classes to your WEB-INF/classes directory and simply write a java class to bind to the JSF front and to expose the web service. I was too lazy to iterate the whole list when showing the incorrect words. so please bare with me π
Example :
package demo;
import com.cdyne.ws.CheckSoap;
import com.cdyne.ws.Words;
import java.util.List;public class SpellChecker {
private CheckSoap service;
private String textArea;
private String wrongWord;public void setWrongWord(String wrongWord) {
this.wrongWord = wrongWord;
}public String getWrongWord() {
return wrongWord;
}public void setTextArea(String textArea) {
this.textArea = textArea;
}public String getTextArea() {
return textArea;
}//the spring context sets the service using cxf dynamic proxy
public void setService(CheckSoap service) {
this.service = service;
}public void checkSpelling() {
try{
System.out.println(“Checking spelli…”);
com.cdyne.ws.DocumentSummary doc = service.checkTextBody(textArea, “”);
List allwrongwords = doc.getMisspelledWord();
wrongWord = ((Words) allwrongwords.get(0)).getWord();
}catch (Exception e){
e.printStackTrace();
}}
}
Step 3:
Write a small JSF form to display and for use inputs,
Some thing like,
<h:form id=”spellingForm”>
<h:outputLabel id=”label1″ value=”ENTER you text : ” />
<br />
<h:inputTextarea id=”textarea1″ value=”#{spellingBean.textArea}” />
<br />
<h:commandButton id=”cmd1″ value=”check” action=”#{spellingBean.checkSpelling}” />
<br />
<h:outputLabel id=”lab2″ value=”The misspelled word is : ” />
<h:outputText id=”text2″ value=”#{spellingBean.wrongWord}” />
</h:form>
Step 4:
Here comes the good stuff π
Write a Spring Context file for me it was spellapp-servlet.xml
<?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:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd”><bean id=”client” class=”com.cdyne.ws.CheckSoap”
factory-bean=”clientFactory” factory-method=”create”/><bean id=”clientFactory” class=”org.apache.cxf.jaxws.JaxWsProxyFactoryBean”>
<property name=”serviceClass” value=”com.cdyne.ws.CheckSoap”/>
<property name=”address” value=”http://ws.cdyne.com/SpellChecker/check.asmx?WSDL”/>
</bean></beans>
Step 5:
The Faces config (faces-config.xml)
<faces-config version=”1.2″
xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd”>
<application>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application><managed-bean>
<managed-bean-name>spellingBean</managed-bean-name>
<managed-bean-class>
demo.SpellChecker
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>service</property-name>
<value>#{client}</value>
</managed-property></managed-bean>
</faces-config>
Step 6:
The web.xml
<web-app version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<!– Spring Application Context configuration –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spellapp-servlet.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param><context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 7:
That’s It Build it deploy it. Have fun π