cxf WebService整合Spring
2015-07-31 15:22
501 查看
第一步创建web项目
可以把apache-cxf-3.1.1中lib可以全部拷贝到项目中lib文件夹
http://download.csdn.net/detail/duankelin/8949701 cxf下载
编写WebService 服务端
1.实体类
2.WebService 接口方法 (最终暴露在外交由别人使用的方法)
3.接口实现
4.编写websevices 发布文件bean.xml
5.web.xml配置
6.启动项目 自动完成webservices发布,显示结果如下:
请求地址:http://127.0.0.1:8089/cxf_Spring/orderws?wsdl 与bean.xm中的address对应
二 客户端测试
1.新建web项目,利用cxf工具生客户端代码,方法如下
运行cmd 命令,在dos下进入项目src下
执行命令wsdl2java wsdl 文件 回车就ok了 刷新项目 就能看到生成的代码了
如果出现不是内部命令的提示 则需要在系统变量PATH中配置cxf 如:D:\apache-cxf-3.1.1\bin
2.编写客户端client-bean.xml文件
两种方法都可以,最终结果如下:
可以把apache-cxf-3.1.1中lib可以全部拷贝到项目中lib文件夹
http://download.csdn.net/detail/duankelin/8949701 cxf下载
编写WebService 服务端
1.实体类
public class Order { private int id; private String name; private double price; public Order(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
2.WebService 接口方法 (最终暴露在外交由别人使用的方法)
package com.test.ws; import javax.jws.WebMethod; import javax.jws.WebService; import com.test.bean.Order; @WebService public interface OrderWs { @WebMethod public Order getOrderById(int id); }
3.接口实现
package com.test.ws; import javax.jws.WebService; import com.test.bean.Order; @WebService public class OrderWsImpl implements OrderWs { public OrderWsImpl(){ System.out.println("OrderWsImpl();"); } @Override public Order getOrderById(int id) { System.out.println("server getById"); return new Order(id,"飞机",100000); } }
4.编写websevices 发布文件bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> 这三个文件是cxf的 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="orderWs" id随意 implementor="com.test.ws.OrderWsImpl" <span style="color:#FF0000;">接口实现</span> address="/orderws" /> <span style="color:#FF0000;">WebService 发布路径</span> </beans>
5.web.xml配置
<?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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>cxf_Spring</display-name> <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> <!-- 加载Spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
6.启动项目 自动完成webservices发布,显示结果如下:
请求地址:http://127.0.0.1:8089/cxf_Spring/orderws?wsdl 与bean.xm中的address对应
<?xml version="1.0" encoding="UTF-8"?> -<wsdl:definitions targetNamespace="http://ws.test.com/" name="OrderWsImplService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.test.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> -<wsdl:types> -<xs:schema targetNamespace="http://ws.test.com/" xmlns:tns="http://ws.test.com/" version="1.0" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="getOrderById" type="tns:getOrderById"/> <xs:element name="getOrderByIdResponse" type="tns:getOrderByIdResponse"/> -<xs:complexType name="getOrderById"> -<xs:sequence> <xs:element name="arg0" type="xs:int"/> </xs:sequence> </xs:complexType> -<xs:complexType name="getOrderByIdResponse"> -<xs:sequence> <xs:element name="return" type="tns:order" minOccurs="0"/> </xs:sequence> </xs:complexType> -<xs:complexType name="order"> -<xs:sequence> <xs:element name="id" type="xs:int"/> <xs:element name="name" type="xs:string" minOccurs="0"/> <xs:element name="price" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> -<wsdl:message name="getOrderByIdResponse"> <wsdl:part name="parameters" element="tns:getOrderByIdResponse"> </wsdl:part> </wsdl:message> -<wsdl:message name="getOrderById"> <wsdl:part name="parameters" element="tns:getOrderById"> </wsdl:part> </wsdl:message> -<wsdl:portType name="OrderWs"> -<wsdl:operation name="getOrderById"> <wsdl:input name="getOrderById" message="tns:getOrderById"> </wsdl:input> <wsdl:output name="getOrderByIdResponse" message="tns:getOrderByIdResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> -<wsdl:binding name="OrderWsImplServiceSoapBinding" type="tns:OrderWs"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> -<wsdl:operation name="getOrderById"> <soap:operation style="document" soapAction=""/> -<wsdl:input name="getOrderById"> <soap:body use="literal"/> </wsdl:input> -<wsdl:output name="getOrderByIdResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:service name="OrderWsImplService"> -<wsdl:port name="OrderWsImplPort" binding="tns:OrderWsImplServiceSoapBinding"> <soap:address location="http://127.0.0.1:8089/cxf_Spring/orderws"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
二 客户端测试
1.新建web项目,利用cxf工具生客户端代码,方法如下
运行cmd 命令,在dos下进入项目src下
执行命令wsdl2java wsdl 文件 回车就ok了 刷新项目 就能看到生成的代码了
如果出现不是内部命令的提示 则需要在系统变量PATH中配置cxf 如:D:\apache-cxf-3.1.1\bin
2.编写客户端client-bean.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> 4000 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:client id="<span style="color:#FF0000;">orderClient</span>" serviceClass="<span style="color:#FF0000;">com.test.ws.OrderWs</span>" <span style="color:#FF0000;">接口全路径</span> address="<span style="color:#FF0000;">http://127.0.0.1:8089/cxf_Spring/orderws</span>"/> 接口地址 </beans>3.编写测试类
package com.test.servserTest; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.ws.Order; import com.test.ws.OrderWs; import com.test.ws.OrderWsImplService; public class Test { public static void main(String[] args) { /*ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"client-bean.xml"}); OrderWs b =(OrderWs) context.getBean("orderClient"); Order order = b.getOrderById(25); System.out.println(order);*/ OrderWsImplService implService = new OrderWsImplService(); OrderWs orderWs = implService.getOrderWsImplPort(); Order a = orderWs.getOrderById(86); System.out.println(a); } }
两种方法都可以,最终结果如下:
相关文章推荐
- Java集合类操作优化总结
- Java导入导出Excel表格
- spring security权限管理手册
- JDK API 1.6中文版和英文版,1.7,1.8英文版分享
- Java在不同的编码下面,一个中文占的字节不同
- 分享java打印简单图形的实现代码
- java.lang.OutOfMemoryError: unable to create new native thread解决方法
- 深入剖析Java中的各种异常处理方式
- 使用springmvc上传文件
- java获取桌面,我的文档路径
- spring mvc使用@InitBinder 标签对表单数据绑定
- eclipse 无线调试
- struts2命名空间namespace和访问路径的关系
- 关于springmvc上传图片
- java学习基础
- 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示
- 爸爸和儿子的故事带你理解java线程
- Spring MVC 学习 之 - URL参数传递
- Java进阶(九)正则表达式
- Java进阶(九)正则表达式