WebService学习笔记-使用CXF编写基于Spring的Webservice
来源:互联网 发布:淘宝是猫京东是狗 编辑:程序博客网 时间:2024/05/17 04:52
新建Web工程,目录结构如图
Order.java实体类
public class Order {private int id;private String name;private double price;///...省略getter setter toString方法public Order() {//无参构造函数super();}}
OrderWS.java接口
package com.umgsai.ws;import javax.jws.WebMethod;import javax.jws.WebService;import com.umgsai.beans.Order;@WebServicepublic interface OrderWS {@WebMethodpublic Order getOrderById(int id);}
OrderWSImpl.java实现接口
package com.umgsai.ws.impl;import com.umgsai.beans.Order;import com.umgsai.ws.OrderWS;public class OrderWSImpl implements OrderWS{@Overridepublic Order getOrderById(int id) {System.out.println("Server getOrderById:" + id);return new Order(id, "Car", 200000);}}
beans.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: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"> <!-- 引入CXF的核心配置 --> <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="orderWS" implementor="com.umgsai.ws.impl.OrderWSImpl" address="/orderWS" /></beans>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>WS_spring</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置beans --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 应用启动监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 所有的请求都会先经过CXF框架 --> <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></web-app>
启动项目
http://localhost:8080/WS_spring/orderWS?wsdl 可以查看发布的服务
源码 http://yunpan.cn/cg4kLXaK8k5HS (提取码:7c34)
客户端调用上面发布的服务。
使用CXF自带的工具生成客户端代码
apache-cxf-3.0.1\bin>wsdl2java http://localhost:8080/WS_spring/orderWS?wsdl
client-beans-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:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="orderClient" serviceClass="com.umgsai.ws.OrderWS"address="http://localhost:8080/WS_spring/orderWS" /></beans>
client.java 客户端代码
package com.umgsai.ws.client;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.umgsai.ws.Order;import com.umgsai.ws.OrderWS;public class Client {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("client-beans.xml");OrderWS orderWS = (OrderWS) applicationContext.getBean("orderClient");Order order = orderWS.getOrderById(23);System.out.println(order);}}
客户端打印结果
Order [id=23, name=Car, price=200000.0]
服务器端打印结果
Server getOrderById:23
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1563774
0 0
- WebService学习笔记-使用CXF编写基于Spring的Webservice
- 用cxf编写基于spring的webservice之上篇
- 用cxf编写基于spring的webservice之下篇
- WebService学习笔记-使用CXF发布Webservice
- 使用CXF编写WebService
- spring中发布基于cxf的webservice
- webservice 基于spring的cxf发布
- WebService学习——使用CXF编写WebService服务端
- WebService学习——使用CXF编写WebService客户端
- webservice学习笔记 --- day03 CXF+Spring+REST
- WebService CXF学习笔记
- Web Service (012---webservice使用CXF写基于Spring的WebService[服务端+客户端])
- WebService学习笔记:使用CXF+Spring搭建WebService服务端和客户端
- 基于Spring+CXF技术开发WebService
- CXF 创建 WebService (基于 Spring)
- webservice学习之使用Apache CXF和spring搭建webservice
- WebService-CXF-Spring基于web的cxf
- WebService学习笔记-CXF支持的数据类型
- WebService学习笔记-使用CXF发布Webservice
- WebService学习笔记-WSDL文档分析
- WebService学习笔记-CXF支持的数据类型
- WebService学习笔记-CXF添加拦截器
- WebService学习笔记-CXF添加自定义拦截器
- WebService学习笔记-使用CXF编写基于Spring的Webservice
- symbol lookup error: undefined symbol
- WebService学习笔记-添加拦截器到基于Spring的Webservice
- WebService学习笔记-Ajax请求Webservice
- WebService学习笔记-Ajax通过servlet请求跨域Webservice
- JUnit学习笔记-入门介绍
- UML学习笔记
- UILabel的缩放效果
- JavaScript DOM编程 学习笔记