zoukankan      html  css  js  c++  java
  • 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());
    				}


  • 相关阅读:
    [No0000188][VCB-Studio 科普教程 2.5] 基于 PotPlayer 和 madVR 的播放器教程(已更新 XySubFilter)
    [No0000184]JAVA语言学校的危险性
    [No0000185]Java技术板块图
    [No0000187]可能是把Java内存区域讲的最清楚的一篇文章
    [No0000183]Parallel Programming with .NET-How PLINQ processes an IEnumerable<T> on multiple cores
    [No0000182]Parallel Programming with .NET-Partitioning in PLINQ
    [No0000179]改善C#程序的建议2:C#中dynamic的正确用法
    [No0000178]改善C#程序的建议1:非用ICloneable不可的理由
    [No0000181]改善C#程序的建议9:使用Task代替ThreadPool和Thread
    [No000017E]改善C#程序的建议7:正确停止线程
  • 原文地址:https://www.cnblogs.com/gwd1154978352/p/6831920.html
Copyright © 2011-2022 走看看