- 开发环境准备:
- Tomcat 8
- Eclipse 4.5.2
- Apache-cxf 的jar包(用于在eclipse 中配置CXF Runtime),这里我使用的版本是3.1.10
- 新建一个动态web工程server(作为Server)
- 写入代码:
- dto类
package com.hand.cxf_server.dto;
import javax.jws.WebService;
@WebService public class Order {
private int id; private String name; private double 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; }
@Override public String toString() { return "OrderWS [id=" + id + ", name=" + name + ", price=" + price + "]"; }
public Order(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; }
} |
- Interface接口OrderWS
package com.hand.cxf_server.ws.service;
import javax.jws.WebMethod; import javax.jws.WebService;
import com.hand.cxf_server.dto.Order;
@WebService public interface OrderWS {
@WebMethod public Order getOrderById(int id); } |
- 实现类
package com.hand.cxf_server.ws.service.impl;
import javax.jws.WebService;
import com.hand.cxf_server.dto.Order; import com.hand.cxf_server.ws.service.OrderWS;
public class OrderWSImpl implements OrderWS {
public Order getOrderById(int id) { System.out.println("Server getOrderById(): "+id); return new Order(id, "飞机", 999999999); }
} |
- OrderWS 右键->Web Service ->Create Web Service
- a. 修改服务器和Service接口
-
- 按步骤next,直至创建完毕
- 启动tomcat服务器,发布服务
- 如果遇到错误prefix wsdp is not bound to a namespace
- 解决办法:删除cxf库中前缀为cxf-services的jar包
- 如果没有错误,在浏览器中输入http://localhost:8080/server/services/orderws?wsdl,检查是否发布成功
- 新建动态web工程(Client)
- )
- 右键New->Other->Web Service Client
- 新建cxf-client.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.hand.cxf_server.ws.service.OrderWS" address="http://localhost:8080/server/services/orderws"></jaxws:client> </beans> |
- 新建测试类Main
package com.hand.cxf_server.ws.service.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hand.cxf_server.ws.service.Order; import com.hand.cxf_server.ws.service.OrderWS;
public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("cxf-client.xml"); OrderWS orderWS = (OrderWS) context.getBean("orderClient"); Order order = orderWS.getOrderById(33); System.out.println(order); } } |
- 运行查看结果
- 备注:
- 如果使用maven部署项目,则maven依赖如下
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> |
- 跨域问题:可以使用HttpURLConnection请求webservice