一:CXF 的开发环境: 1、CXF版本:2.7.8 ,官网:
http://cxf.apache.org/download.html (提示:不要下载最新版本的apache-cxf-3.0.0,本人有这个血泪史,请看上篇文章。) 2、Eclipse版本:3.5 3、JDK :1.6 4、Tomcat版本:apache-tomcat-7.0.42
二:搭建CXF Demo
1、新建Web工程,这里命名为cxfservice
2、将CXF文件下Lib文件夹中的jar包导入到工程中
3、web.xml集成Spring、CXF配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cxfdemo</display-name>
<context-param>
<param-value>classpath:applicationContext.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>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4、[size=12px; line-height: 25.1875px;" data-mce-mark="1]在工程中新建包com.credit56.service.cxf.service,在包中新建inteface,名称为IGreetingService:[/size]
import com.credit56.service.cxf.model.Customer;
/**
*
*
* 功能:Hello World web service Demo
*
*
* @ClassName: HelloWordService
* @version V1.0
* @date 2013年12月18日
* @author qinhailong
*/
public interface IGreetingService {
public String greeting(String name);
public Customer getCustomer(Customer customer);
public String getCustomerOfJSON();
}
5、编写接口实现类GreetingServiceImpl
package com.credit56.service.cxf.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import net.sf.json.JSONObject;
import com.credit56.service.cxf.model.Customer;
import com.credit56.service.cxf.service.IGreetingService;
/**
*
* 功能:web service Demo
*
*
* @ClassName: HelloWorldServiceImpl
* @version V1.0
* @date 2013年12月18日
* @author qinhailong
*/
@WebService
public class GreetingServiceImpl implements IGreetingService {
/**
*
* 功能:问候语
*
*
* @author qinhailong
* @date 2013年12月18日
* @param name
* @return
* @see com.credit56.demo.cxf.service.IGreetingService#sayHello(java.lang.String)
*/
@Override
@WebMethod
public String greeting(@WebParam(name = "name")
String name) {
return new StringBuffer(name).append(",Hello Word! currentTime is ")
.append(new SimpleDateFormat("yyyy-MM-dd").format(new Date())).toString();
}
/**
*
*
* 功能:返回Customer
*
*
* @author qinhailong
* @date 2013年12月19日
* @param customer
* @return
* @see com.credit56.demo.cxf.service.IGreetingService#getCustomer(com.credit56.demo.cxf.model.Customer)
*/
@Override
@WebMethod
public Customer getCustomer(@WebParam(name = "customer")
Customer customer) {
if (null != customer.getId()) {
return customer;
}
Customer c = new Customer();
c.setId(1L);
c.setBirthday(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
c.setName("shuqin_wang");
return c;
}
/**
*
*
* 功能:返回客户信息JOSN
*
*
* @author qinhailong
* @date 2013年12月19日
* @return
* @see com.credit56.demo.cxf.service.IGreetingService#getCustomerOfJSON()
*/
@Override
@WebMethod
public String getCustomerOfJSON() {
Customer customer = this.getCustomer(new Customer());
JSONObject jsonObject = JSONObject.fromObject(customer);
return jsonObject.toString();
}
}
6、与Spring 集成(applicationContext.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"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.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:endpoint id="greetingService"
implementor="com.credit56.demo.cxf.service.impl.GreetingServiceImpl"
address="/greetingService">
</jaxws:endpoint>
</beans>
7、部署项目,启动tomcat,访问:http:localhost:8080/cxfservice
点击{http://impl.service.cxf.demo.credit56.com/}GreetingServiceImplService
8、说明CXF发布已经成功。
二、客户端调用
新建cxfclient项目,过程与上类似,但为了方便,就建Application,其中过程不重复。
1、新建一个interface,与上相同
package com.credit56.demo.cxf.client.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.credit56.demo.cxf.client.model.Customer;
/**
*
* <p class="detail">
* 功能:Hello World web service Demo
*
*
* @ClassName: HelloWordService
* @version V1.0
* @date 2013年12月18日
* @author [url=mailto:[email protected]]qinhailong[/url]
*/
@WebService
public interface IGreetingService {
@WebMethod
// public String sayHello(@WebParam(name="name") String name);
// 通过@WebParam(name="***")来设置WSDL中的参数名称,默认为arg0、arg1......
public String greeting(@WebParam(name = "name") String name);
@WebMethod
public Customer getCustomer(@WebParam(name = "customer") Customer customer);
@WebMethod
public String getCustomerOfJSON();
}
2、CXF与Spring集成applicationContext.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"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<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" />
<!-- 通过与服务端配置相对的 CXF 标签 <jaxws:client> 来定义客户端访问服务的声明 -->
<jaxws:client id="greetingServiceClient"
serviceClass="com.credit56.demo.cxf.client.service.IGreetingService"
address="http://localhost:8080/cxfdemo/greetingService">
</jaxws:client>
</beans>
3、客户端main调用:
package com.credit56.demo.cxf.client;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.credit56.demo.cxf.client.model.Customer;
import com.credit56.demo.cxf.client.service.IGreetingService;
//org/apache/xml/security/resource/xmlsecurity
public class CXFTest {
public static void main(String[] args) {
System.out.println("======"+ System.getProperty("java.endorsed.dirs"));
IGreetingService greetingServiceClient = (IGreetingService) new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml").getBean("greetingServiceClient");
greetingServiceClient.greeting("shuqin_wang");
Customer customer = new Customer();
customer.setId(1L);
customer.setBirthday(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
customer.setName("hailong_qin");
Customer c = greetingServiceClient.getCustomer(customer);
System.out.println("id:" + c.getId() + " name:" + c.getName() + " birthday:" + c.getBirthday());
System.out.println(greetingServiceClient.getCustomerOfJSON());
}
}
4、完整代码见附件