CXF使用教程(三)——基于Spring的webService开发


server端

1.建立web工程,导入相关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);
	}
}

3.配置Spring_Server_CXF.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>
		


client端:

1.新建web工程

2.根据wsdl文档生成客户端代码

3.配置Spring_Client_CXF.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(
							"Spring_Client_CXF.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());
				}


原文地址:https://www.cnblogs.com/gwd1154978352/p/6831920.html