场景
实现
搭建服务端
1.打开Eclipse--新建Dynamic Project--CXF_Spring_Web
2.导入cxf的jar包
jar包位置:
将lib下的所有jar包文件导入,虽然不都用得到但是先暂时都导入。
然后右击lib包右击build path
3.新建接口以及实现类
在src下新建包,先在包下新建实体类
package com.badao.bean;
public class Order {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Order(int id, String name, double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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;
}
private int id;
private String name;
private double price;
}
然后在包下新建接口OrderWS
package com.badao.cxf;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.badao.bean.Order;
@WebService
public interface OrderWS {
@WebMethod
public Order getOrderById(int id);
}
接口上要添加注解@WebService
方法上要添加注解 @WebMethod
然后新建实现类
package com.badao.cxf;
import javax.jws.WebService;
import com.badao.bean.Order;
@WebService
public class OrderWSImpl implements OrderWS {
@Override
public Order getOrderById(int id) {
System.out.println("服务端获取的id为:"+id);
return new Order(id,"订单1",100000.0);
}
}
接口实现上要添加注解@WebService
4.编写配置文件beans.xml
依据文档,需要编写一个beans.xml配置文件。
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的核心bean
-->
<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" />
<!--
终端:发布webservice(将SEI的实现类对象与web service所提供的网络地址关联)
address="/orderWS":配置虚拟路径
-->
<jaxws:endpoint id="orderWS" implementor="com.badao.cxf.OrderWSImpl"
address="/orderWS" />
</beans>
5.修改web.xml
依据文档
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CXF_Spring_Web</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>
<!-- 配置上下文初始化参数:指定cxf的spring的beans.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 加载上面参数的配置文件的listener -->
<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>
运行服务端
将项目部署到Tomcat上。
打开浏览器访问:
http://localhost:8080/CXF_Spring_Web/orderWS
查看wsdl文件
打开浏览器访问:
http://localhost:8080/CXF_Spring_Web/orderWS?wsdl
至此则服务端部署完成。
服务端项目结构目录
客户端实现
1.新建项目
Eclipse新建Dynamic project
同上面一样导入jar包。
2.根据wsdl文档生成客户端代码
下载apache-cxf-2.5.9,在bin目录下有wsdl2java.bat
将其配置进环境变量。
进入到客户端的真实目录下的src下。
在此处打开cmd,输入:
wsdl2java http://localhost:8080/CXF_Spring_Web/orderWS?wsdl
可以生成客户端代码。
此时回到Eclipse 下刷新项目。
编写客户端配置文件
依据文档,在src下新建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/jaxws">
<!-- serviceClass:是根据服务端wsdl生成代码的接口的位置
address:是上面服务端发布的address
-->
<jaxws:client id="orderClient"
serviceClass= "com.badao.cxf.OrderWS"
address= "http://localhost:8080/CXF_Spring_Web/orderWS">
</jaxws:client>
</beans>
编写客户端代码
新建ClientTest.java
package com.badao.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.badao.cxf.Order;
import com.badao.cxf.OrderWS;
public class ClientTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
OrderWS orderWS = (OrderWS) context.getBean("orderClient");
Order order = orderWS.getOrderById(24);
System.out.println("服务端返回的数据为:"+order.getId()+order.getName()+order.getPrice());
}
}
客户端项目结构目录
效果
将服务端代码部署到Tomcat上并运行,然后运行客户端的ClientTest方法。
客户端运行效果:
服务端效果: