Tutorial66.com
@Share Application Development Skill.



Tutorial is tutorial...66 is My Country Code Up to 10 Years EXP for Application Development Role in Thailand.
collapse

Tutorial Category



Topic: Java web services tutorial with Spring Framework and Apache CXF  (Read 200 times)


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
Java web services tutorial with Spring Framework and Apache CXF
« on: October 21, 2011, 01:00:09 AM »
Creating a Webservice using Java is not difficult anymore.   ::)
Hello Guy This is My First Tutorial.
Requirement Tools
 1. JDK and Eclipse Editor (it 's common that we have) ;D
 2. Apache CXF  .. Go to Google and Search with "Apache CXF Download"  to get it
 3. Spring Framework ..I believe you know.but if it does not goto Google and Search "Spring Framework Download" 
 
Remark :Step Over ..is Common Reply this topic If you do not.  :'(
Remark :Step Into ..I Will Show you in detail and Example.  8)

Step 1 : Use Eclipse IDE to create Dynamic Web Application (Step Over)
Step 2 : Import Apache CXF Library and Spring Spring Framework  to Project  (Step Over)
Step 3 : Build Service Class (Step Into)
Build Service InterFace
Code: [Select]
package com.en.ws;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.en.dto.ExampleDto;
/**
 * @author Somsuksri Kasemsuk
 * @version 1.0 $Id: ExampleWSInf.java 2693 2011-07-03 $
 */
@WebService
public interface ExampleWSInf {
public ExampleDto getData(@WebParam(name="codeId")String codeId);
}


Build Service Class
Code: [Select]
package com.en.ws;

import java.io.Serializable;

import com.en.dto.ExampleDto;
import com.en.model.MasterModel;

//@WebService(endpointInterface = "com.en.ws.ExampleWSInf", serviceName = "ExampleWS", portName="ExampleWS")
public class ExampleWS  implements ExampleWSInf,Serializable {
private static final long serialVersionUID = 1L;
private MasterModel model;
public ExampleDto getData(String codeId){
ExampleDto dto=(ExampleDto)model.getDataObj(new Integer(codeId));
return dto;
}
public MasterModel getModel() {
return model;
}
public void setModel(MasterModel model) {
this.model = model;
}

}

Optional Class
DTO , Model is Concept for MVC (Step Over)

Step 4 :Spring  XML Configuration(Step Into)
Create XML Configuration file  for EXample : applicationCfxContext-WS.xml
Code: [Select]
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://cxf.apache.org/jaxws
  http://cxf.apache.org/schemas/jaxws.xsd">

<!-- Load CXF modules from cxf.jar -->   
<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" />

<!-- Can Binding Resource to Service -->
<bean id="exampleWS" class="com.en.ws.ExampleWS">
<property name="model" ref="exampleModel"/>
</bean>
<jaxws:endpoint id="EXAMPLEWS"
implementorClass="com.en.ws.ExampleWS"
implementor="#exampleWS"
address="/example" />

</beans>

Step 5 :web.xml Configuration(Step Into)
Add Apache CXF Servlet
Code: [Select]
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

Load Service Class Bean Configuration To context-param
Code: [Select]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/appcontext/applicationCfxContext-WS.xml
</param-value>
</context-param>
« Last Edit: October 21, 2011, 03:43:33 AM by Admin »




SimplePortal 2.3.3 © 2008-2010, SimplePortal