一: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:engineer01@great-tao.com]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、完整代码见附件
- 大小: 77.4 KB
- 大小: 51.3 KB
- 大小: 42.6 KB
- 大小: 146.3 KB
分享到:
相关推荐
webserivce.、cxf整合spring,用的是spring3和cxf3。欢迎大家下载尝试
CXF和Spring搭建webservice代码
WebService的CXF整合Spring有源码和jar包
使用的是CXF+Spring搭建,后台DB层使用ibatis ,数据库使用Mysql 5.5,后台与页面传输使用的是Json数据格式.
自己用做项目时用到CXF整合Spring,写了下步骤,希望能帮到有需要的人!
CXF整合Spring需要用到的JAR包
CXF Spring Web Service 程序,希望对大家有用!!
绝对可运行,里面有客户端和服务端还有对应的操作文档,导入eclipse或者MyEclipse十分钟内就搞定,虽然贵点但是绝对物有所值.
cxf整合spring的webservices jar包下载,里面的jar包最好一起复制黏贴,这个也可能会有版本冲突问题
CXF整合spring实现REST接口开发
CXF整合spring,同时支持JSON和XML的CXF配置,支持自动转成实体类,其中的hibernate配置可以不用理会。如果还有不明白可以在留言里留下联系方式,大家可以一起讨论
cxf+spring整合,只要把项目导入到myeclipse中,就可以运行。
CXF整合spring实现SOAP接口开发
CXF整合Spring Spring 整合CXF开发服务 准备工作:下载相应的jar包(cxf和spring)
Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
最完整的的cxf与spring整合需要的jar文件
linux环境下,cxf整合spring发布webservice源码
Springboot整合CXF发布Web service和客户端调用(用户和密码验证)打开及用