用cxf做基于spring的web service开发
1.server端:
1. 新建web工程,导入cxf的jar包
2. 定义SEI和其实现
@WebService
public interface OrderService {
@WebMethod
public Order getOrderByNo(String orderNo);
}
@WebService
public class OrderServiceImpl implements OrderService {
public OrderServiceImpl() {
System.out.println("OrderServiceImpl()");
}
@Override
public Order getOrderByNo(String orderNo) {
System.out.println("getOrderByNo() orderNO=" + orderNo);
return new Order(3, orderNo, 10000000);
}
}
public class Order {
private int id;
private String orderNo;
private double price;
}
3. 发布web service
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所提供的网络地址关联)
-->
<jaxws:endpoint id="orderService" implementor="net.atguigu.java.ws_spring.ws.OrderServiceImpl"
address="/orderService" />
</beans>
4.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>day101_ws_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>
<!-- 配置上下文初始化参数:指定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>
2.client端
1. 新建web工程,导入cxf的jar包
2. 根据wsdl文档生成客户端代码(src下)
wsdl2java –d . http://127.0.0.1:9999/weather?wsdl
3. client-beans.xml
<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="net.atguigu.java.ws_spring.ws.OrderService"
address="http://localhost:8080/day101_ws_spring/orderService" />
</beans>
4. 编写测试代码:
public static void main(String[] args) {
//加载client-beans.xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"client-beans.xml");
//根据bean的id加载所对应的OrderService
OrderService orderService = (OrderService) context.getBean("orderClient");
//调用方法,发送soap请求
Order order = orderService.getOrderByNo("hao123");
System.out.println("client " + order.getId() + "_" + order.getOrderNo()
+ "_" + order.getPrice());
}
5. 添加拦截器
客户端:
<jaxws:client id="orderClient" serviceClass="com.atguigu.day01_ws_server.ws.OrderWs"
address="http://localhost:8989/day02_cxf_spring_server/orderws">
<jaxws:outInterceptors>
<bean class="com.atguigu.day01_ws_server.ws.intercepter.AddUserIntercepter">
<property name="name" value="xfzhang"></property>
<property name="pwd" value="123456"></property>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
服务器端:
<jaxws:endpoint id="orderWs"
implementor="com.atguigu.day01_ws_server.ws.OrderWsImpl" address="/orderws">
<jaxws:inInterceptors>
<bean class="com.atguigu.day01_ws_server.intercepter.CheckUserIntercepter"/>
</jaxws:inInterceptors>
</jaxws:endpoint>