用cxf做基于spring的web service开发(八)

用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 . 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>


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值