WebService – CXF 与Spring整合(Service+Client)

【1】编写服务端代码

① 项目结构


② service

package com.web.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.web.bean.Order;
//通过注解@WebService申明为webservice接口 
@WebService
public interface OrderWS {
    //@WebMethod 接口的方法
    //@WebParam给参数命名,提高可代码可读性,此项可选
    @WebMethod
    public Order getOrderById(@WebParam(name="id") int id);
}

③ impl

package com.web.service.impl;

import javax.jws.WebService;

import com.web.bean.Order;
import com.web.service.OrderWS;

@WebService
public class OrderWSImpl implements OrderWS{

    public OrderWSImpl() {
        // TODO Auto-generated constructor stub
        System.out.println("OrderWSImpl is inited ...");
    }

    @Override
    public Order getOrderById(int id) {
        System.out.println("server getOrderById"+id);
        return new Order(id, "tom", 12.02);
    }

}

④ bean-order

package com.web.bean;

public class Order {

    private int id;
    private String name;
    private double price;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
    public Order(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    public Order() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
    }


}

【2】配置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/jaxws">


  
    
   
    

   
   <jaxws:endpoint 
     id="orderWS" 
     implementor="com.web.service.impl.OrderWSImpl" 
     address="/orderws">
    

【3】配置web.xml



  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

  
  
      contextConfigLocation
      classpath:beans.xml
   

   <!-- 
        应用启动的一个监听器
    -->
   
      
         org.springframework.web.context.ContextLoaderListener
      
   

   <!-- 
        所有请求都会先经过cxf框架
    -->
   
      CXFServlet
      
         org.apache.cxf.transport.servlet.CXFServlet
      
      1
   
   
      CXFServlet
      /*
    

【4】发布到tomcat


访问WSDL:

http://localhost:8080/WebServserSpringCXF2/orderws?wsdl

// /orderws前面的好理解,"/orderws"就是endpoint中定义的,"?wsdl"就不多说了


WSDL文件如下:


  
    
  
    
    
      
      
        
      
      
        
      
    
  
  
    
      
    
  

【5】新建客户端项目 ,进行客户端编写与调试

① 新建客户端项目,根据wsdl生成Stub

过程参考:使用JDK(jetty)生成Stub进行webservice开发

② 配置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/jaxws">

    
    <jaxws:client id="orderClient" 
        serviceClass= "com.web.service.OrderWS" 
        address= "http://localhost:8080/WebServserSpringCXF2/orderws">
    

③ 编写客户端测试类

packagepackage com.web.Client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.web.service.Order;
import com.web.service.OrderWS;

public class ClientTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
        OrderWS orderWS = (OrderWS) context.getBean("orderClient");
        Order orderById = orderWS.getOrderById(1);
        System.out.println("Client "+orderById);
    }
}

运行并查看客户端输出结果:


查看服务端输出结果:

发表评论