wsdl文档结构图

  1. JDK方式开发

server端代码

package server;import javax.jws.WebMethod;import javax.jws.WebService;/** *  *  * SEI:Service Endpoint Interface 发布的服务接口 * */@WebServicepublic interface HelloWS {@WebMethod    public String sayHello(String name);}
package server;import javax.jws.WebService;/** *  *  * SEI的实现 * */@WebServicepublic class  HelloWSImpl  implements HelloWS{    @Override    public String sayHello(String name) {        System.out.println("server sayHello()"+name);        return "Hello"+name;    }}
package server;import javax.xml.ws.Endpoint;public class ServerPublish {        public static void main(String[] args) {                String address="http://localhost:8090/sayhello";        //对应的wsdl文档地址http://localhost:8090/sayhello?wsdl        Endpoint.publish(address, new HelloWSImpl());        System.out.println("发布服务成功");    }}

根据wsdl文件生成客户端代码

远程wsimport -keep .net代码写的需要修改里面的
    
    
为  
    
  
 本地wsimport -keep  WeatherWS.wsdl

使用以下代码发送请求

package weather;import java.util.List;import cn.com.webxml.ArrayOfString;import cn.com.webxml.WeatherWS;import cn.com.webxml.WeatherWSSoap;public class ClientTest {    public static void main(String[] args) {                WeatherWS weatherWS  =new WeatherWS();                WeatherWSSoap  weatherWSSoap =weatherWS.getWeatherWSSoap();        ArrayOfString  arrayOfString= weatherWSSoap.getWeather("上海", null);        List
 list=arrayOfString.getString();        System.out.println(list);        for(String s:list){                        System.out.println(s+"***");        }            }}

2.CXF方式

CXF支持map,jdk自带不支持map数据结构

使用wsdl2java +wsdl文件地址  生成客户端调用代码

cxf3.1.4添加日志服务器和客户端拦截器

服务器

package server;import java.util.List;import org.apache.cxf.frontend.ServerFactoryBean;import org.apache.cxf.interceptor.Interceptor;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.message.Message;public class ServerPublish {        public static void main(String[] args) {                String address="http://localhost:8090/sayhello";        HelloWSImpl hw = new HelloWSImpl();        ServerFactoryBean sfb = new ServerFactoryBean();        sfb.setServiceClass(HelloWS.class);        sfb.setServiceBean(hw);        sfb.setAddress(address);        //服务器端的日志入拦截器        List
> inInterceptors =sfb.getInInterceptors();        inInterceptors.add(new LoggingInInterceptor() );              //服务器端的日志出拦截器        List
> outInterceptors =sfb.getOutInterceptors();        outInterceptors.add(new LoggingOutInterceptor());        sfb.create();        System.out.println("发布服务成功");    }}

客户端

package client;import java.util.List;import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.interceptor.Interceptor;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.message.Message;import server.HelloWS;import server.HelloWSPortType;public class client_test {    public static void main(String[] args) {        HelloWS helloWS = new HelloWS();        HelloWSPortType helloWSPortType = helloWS.getHelloWSPort();        Client client=ClientProxy.getClient(helloWSPortType);         //客户端的日志入拦截器        List
> inInterceptors =client.getInInterceptors();        inInterceptors.add(new LoggingInInterceptor() );              //客户端的日志出拦截器        List
> outInterceptors =client.getOutInterceptors();        outInterceptors.add(new LoggingOutInterceptor());                String s=helloWSPortType.sayHello(" yuanhai");                System.out.println(s);    }}

cxf添加自定义拦截器实现密码用户名验证

客户端拦截器

package client;import java.util.List;import javax.xml.namespace.QName;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.headers.Header;import org.apache.cxf.helpers.DOMUtils;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.phase.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.w3c.dom.Document;import org.w3c.dom.Element;public class AddUserInterceptor  extends AbstractPhaseInterceptor
{        private String name;    private String password;    public AddUserInterceptor(String name, String password) {        super(Phase.PRE_PROTOCOL);//准备协议化时拦截        this.name=name;        this.password=password;                }        /*     在soap中加个head     
              
             
yuanhai
             
123456
         
         
             
yuanhai
             
123456
         
                   
             
yuanhai
         
               
               */    @Override    public void handleMessage(SoapMessage msg) throws Fault {    List
 headers=    msg.getHeaders();        Document document=DOMUtils.createDocument();        Element rootEle=document.createElement("atguigu");        Element nameEle=document.createElement("name");        nameEle.setTextContent(name);        Element passwordEle=document.createElement("password");        passwordEle.setTextContent(password);        rootEle.appendChild(nameEle);        rootEle.appendChild(passwordEle);                headers.add(new Header(new QName("atguigu"), rootEle));//QName必须跟rootEle的标签一样        System.out.println("client handleMessage()...");    }        }

调用代码

package client;import java.util.List;import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.interceptor.Interceptor;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.message.Message;import server.HelloWS;import server.HelloWSPortType;public class client_test {    public static void main(String[] args) {        HelloWS helloWS = new HelloWS();        HelloWSPortType helloWSPortType = helloWS.getHelloWSPort();                Client client=ClientProxy.getClient(helloWSPortType);         //客户端的日志入拦截器        List
> inInterceptors =client.getInInterceptors();        inInterceptors.add(new LoggingInInterceptor() );              //客户端的日志出拦截器        List
> outInterceptors =client.getOutInterceptors();        outInterceptors.add(new LoggingOutInterceptor());        outInterceptors.add(new AddUserInterceptor("yuanhai","123456"));        String s=helloWSPortType.sayHello(" yuanhai");                System.out.println(s);    }}

服务端拦截器

package server;import javax.xml.namespace.QName;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.headers.Header;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.phase.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.w3c.dom.Element;/* * 检查用户的拦截器 */public class CheckUserInterceptor extends AbstractPhaseInterceptor
 {    public CheckUserInterceptor() {        super(Phase.PRE_PROTOCOL);        }    @Override    public void handleMessage(SoapMessage msg) throws Fault {            Header header=    msg.getHeader(new QName("atguigu"));        if(header!=null){            Element atguiguEle =(Element) header.getObject();                        String name=atguiguEle.getElementsByTagName("name").item(0).getTextContent();            String password=atguiguEle.getElementsByTagName("password").item(0).getTextContent();            if("yuanhai".equals(name)&&"123456".equals(password)){                                System.out.println("Server 通过拦截器...");                return;            }                    }        //不能通过        System.out.println("Server 未通过拦截器...");        throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码"));            }}

调用代码

package server;import java.util.List;import org.apache.cxf.frontend.ServerFactoryBean;import org.apache.cxf.interceptor.Interceptor;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.message.Message;public class ServerPublish {        public static void main(String[] args) {                String address="http://localhost:8090/sayhello";        HelloWSImpl hw = new HelloWSImpl();        ServerFactoryBean sfb = new ServerFactoryBean();        sfb.setServiceClass(HelloWS.class);        sfb.setServiceBean(hw);        sfb.setAddress(address);        //服务器端的日志入拦截器        List
> inInterceptors =sfb.getInInterceptors();        inInterceptors.add(new LoggingInInterceptor() );        inInterceptors.add(new CheckUserInterceptor());      //服务器端的日志出拦截器        List
> outInterceptors =sfb.getOutInterceptors();        outInterceptors.add(new LoggingOutInterceptor());        sfb.create();        System.out.println("发布服务成功");    }}

3.cxf与spring集成

server段代码

实体类order

package bean;public class Order {        public Order(int id, String name, double price) {            this.id = id;        this.name = name;        this.price = price;    }    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;    }    }

webservice接口

package ws;import javax.jws.WebMethod;import javax.jws.WebService;import bean.Order;@WebServicepublic interface OrderWS {        @WebMethod    public Order getOrderById(int id);}

webservice实现类

package ws;import bean.Order;public class OrderWsImpl  implements OrderWS {        public OrderWsImpl(){        System.out.println("OrderWsImpl()");    }    @Override    public Order getOrderById(int id) {                System.out.println("server getOrderById()"+id);        return new Order(id, "飞机", 100000);    }}

spring配置文件beans.xml

                
    
    
    
    
        
    
    
         

web.xml

  
    
contextConfigLocation
    
classpath*:beans.xml
  
  
    
org.springframework.web.context.ContextLoaderListener
  
  
    
CXFServlet
    
org.apache.cxf.transport.servlet.CXFServlet
  
  
    
CXFServlet
    
/*
  

客户端

spring配置文件client-beans.xml

                
        
    
    
    
        
             

客户端调用代码

package test;import org.springframework.context.support.ClassPathXmlApplicationContext;import ws.Order;import ws.OrderWS;public class client_test {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "client-beans.xml" });        OrderWS orderWs = (OrderWS) context.getBean("orderClient");        Order order = orderWs.getOrderById(24);        System.out.println(order);    }}