使用CXF框架开发一个简单的WebService
来源:互联网 发布:it luggage 编辑:程序博客网 时间:2024/06/06 04:01
1.新建一个Web工程做服务端
#代码
Order.java
package com.bean;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;}@Overridepublic String toString() {return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";} }
OrderWS.java
package com.ws;import javax.jws.WebMethod;import javax.jws.WebService;import com.bean.Order;@WebServicepublic interface OrderWS { @WebMethod public Order getOrderById(int id);}OrderWSImpl.java
package com.ws;import javax.jws.WebService;import com.bean.Order;@WebServicepublic class OrderWSImpl implements OrderWS { public OrderWSImpl(){ System.out.println("OrderWSImpl()"); }@Overridepublic Order getOrderById(int id) {// TODO Auto-generated method stubSystem.out.println("server getOrderById() "+id);return new Order(id, "火车票", 20000);}}
#配置文件
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.ws.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://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>CXFWead</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> <!-- 应用启动的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置beans.xml文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 所有的请求都会经过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>
2.新建一个Web工程做客户端
#配置CXF环境变量,输入CMD命令自动生成服务端代码
#配置文件
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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="orderClient" serviceClass="com.ws.OrderWS" address="http://localhost:8013/CXFWead/orderws"/></beans>
ClientTest.java
package com.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ws.Order;import com.ws.OrderWS;public class ClintTest {public static void main(String[] args) {// TODO Auto-generated method stub ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); OrderWS orderWS = (OrderWS) context.getBean("orderClient"); Order order = orderWS.getOrderById(24); System.out.println(order); }}
截图:
WSDL文档结构图
阅读全文
0 0
- 使用CXF框架开发一个简单的WebService
- 使用CXF框架实现webservice的一个简单例子
- webService框架cxf的简单使用
- 使用CXF简单开发webservice的实例
- CXF框架的使用,利用cxf开发webservice(六)
- 一个简单的cxf框架调用webservice接口天气预报例子
- 简单地基于cxf框架的webservice完整开发示例
- CXF webservice的简单使用
- WebService 简单例子,使用CXF框架
- 使用Apache的CXF框架开发WebService实例
- 使用Apache的CXF框架开发WebService实例
- 使用 CXF 写 一个简单的webservice demo
- 使用CXF框架开发WebService客户端
- CXF 创建一个简单的Webservice例子
- webservice框架CXF拦截器的使用
- webservice的CXF框架
- webService 使用CXF 实现简单的helloworld
- webservice使用CXF的简单实现
- JQuery 阻止默认行为
- 378. Kth Smallest Element in a Sorted Matrix
- 小程序与html5开发的差别(连载三)
- H5面试题---介绍js的基本数据类型
- 有趣的if语句
- 使用CXF框架开发一个简单的WebService
- 【博弈+SG函数+打表找规律】HDU_5795_A Simple Nim
- JavaScript学习之简单的放大镜
- Android中View的宽高测量结果为0的原因和解决方法
- 阵列,配额与LVM
- &&重载实现寻找子集
- 【《Real-Time Rendering 3rd》 提炼总结】(六) 第七章 · 高级着色:BRDF及相关技术
- 文件流FileOutputStream类
- H5---js有哪些内置对象