java调用基于SOAP的CXF 框架的WebService客户端和服务器段helloworld例子

时间:2025-02-15 18:41:38

依赖的JAR
    cxf-2.2.
    jetty-6.1.
    jetty-util-6.1.
    servlet-2_5
    wsdl4j-1.6.
    XmlSchema-1.4.
创建一个普通的Java工程即可

创建webservice接口
package  ;

import  ;
import  ;

@WebService
public   interface  HelloWorldServiceInf {
    
    String sayHello(@WebParam(name
= " username " ) String username);
    
}
发布和调用webservice
        方法一
发布webservice
package  ;

import  ;

import  ;
import  ;
import  ;

import  ;

@WebService(endpointInterface
= " " ,serviceName = " helloWorldService " )
public   class  Server  implements  HelloWorldServiceInf {

    
public  String sayHello(String username) {
        
return   " Hello, " + username;
    }

    
    
public   static   void  main(String[] args) {
        Server impl
= new  Server();
        JaxWsServerFactoryBean factoryBean
= new  JaxWsServerFactoryBean();
        (
" http://localhost:9000/hello " );
        (HelloWorldServiceInf.
class );
        (impl);
        ().add(
new  LoggingInInterceptor());
        ().add(
new  LoggingOutInterceptor());
        ();
    }
    
}
wsdl描述文件
   <? xml version="1.0"  ?>  
< wsdl:definitions  name ="HelloWorldServiceInfService"  targetNamespace ="/"  xmlns:ns1 ="/wsdl/soap/http"  xmlns:soap ="/wsdl/soap/"  xmlns:tns ="/"  xmlns:wsdl ="/wsdl/"  xmlns:xsd ="http:///2001/XMLSchema" >
< wsdl:types >
< xsd:schema  attributeFormDefault ="unqualified"  elementFormDefault ="unqualified"  targetNamespace ="/"  xmlns:tns ="/"  xmlns:xsd ="http:///2001/XMLSchema" >
  
< xsd:element  name ="sayHello"  type ="tns:sayHello"   />  
< xsd:complexType  name ="sayHello" >
< xsd:sequence >
  
< xsd:element  minOccurs ="0"  name ="username"  type ="xsd:string"   />  
  
</ xsd:sequence >
  
</ xsd:complexType >
  
< xsd:element  name ="sayHelloResponse"  type ="tns:sayHelloResponse"   />  
< xsd:complexType  name ="sayHelloResponse" >
< xsd:sequence >
  
< xsd:element  minOccurs ="0"  name ="return"  type ="xsd:string"   />  
  
</ xsd:sequence >
  
</ xsd:complexType >
  
</ xsd:schema >
  
</ wsdl:types >
< wsdl:message  name ="sayHelloResponse" >
  
< wsdl:part  element ="tns:sayHelloResponse"  name ="parameters"   />  
  
</ wsdl:message >
< wsdl:message  name ="sayHello" >
  
< wsdl:part  element ="tns:sayHello"  name ="parameters"   />  
  
</ wsdl:message >
< wsdl:portType  name ="HelloWorldServiceInf" >
< wsdl:operation  name ="sayHello" >
  
< wsdl:input  message ="tns:sayHello"  name ="sayHello"   />  
  
< wsdl:output  message ="tns:sayHelloResponse"  name ="sayHelloResponse"   />  
  
</ wsdl:operation >
  
</ wsdl:portType >
< wsdl:binding  name ="HelloWorldServiceInfServiceSoapBinding"  type ="tns:HelloWorldServiceInf" >
  
< soap:binding  style ="document"  transport ="/soap/http"   />  
< wsdl:operation  name ="sayHello" >
  
< soap:operation  soapAction =""  style ="document"   />  
< wsdl:input  name ="sayHello" >
  
< soap:body  use ="literal"   />  
  
</ wsdl:input >
< wsdl:output  name ="sayHelloResponse" >
  
< soap:body  use ="literal"   />  
  
</ wsdl:output >
  
</ wsdl:operation >
  
</ wsdl:binding >
< wsdl:service  name ="HelloWorldServiceInfService" >
< wsdl:port  binding ="tns:HelloWorldServiceInfServiceSoapBinding"  name ="HelloWorldServiceInfPort" >
  
< soap:address  location ="http://localhost:9000/hello"   />  
  
</ wsdl:port >
  
</ wsdl:service >
  
</ wsdl:definitions >
客户端调用
package  ;

import  ;
import  ;
import  ;
import  ;

public   class  Client {
    
public   static   void  main(String[] args) {
        JaxWsProxyFactoryBean  factoryBean
= new  JaxWsProxyFactoryBean();
        ().add(
new  LoggingInInterceptor());
        ().add(
new  LoggingOutInterceptor());
        (HelloWorldServiceInf.
class );
        (
" http://localhost:9000/hello " );
        HelloWorldServiceInf impl
= (HelloWorldServiceInf) ();
        ((
" 张三 " ));
    }
}
        方法二
发布webservice
package  ;

import  ;
import  ;

import  ;

@WebService(endpointInterface
= " " ,serviceName = " helloWorldService " )
public   class  Server  implements  HelloWorldServiceInf {

    
public  String sayHello(String username) {
        
return   " Hello, " + username;
    }
    
public   static   void  main(String[] args) {
        Server impl
= new  Server();
        String address
= " http://localhost:9000/hello " ;
        (address, impl);
    }
}
wsdl文件
   <? xml version="1.0"  ?>  
< wsdl:definitions  name ="helloWorldService"  targetNamespace ="/"  xmlns:ns1 ="/"  xmlns:ns2 ="/wsdl/soap/http"  xmlns:soap ="/wsdl/soap/"  xmlns:tns ="/"  xmlns:wsdl ="/wsdl/"  xmlns:xsd ="http:///2001/XMLSchema" >
  
< wsdl:import  location ="http://localhost:9000/hello?wsdl="  namespace ="/"   />  
< wsdl:binding  name ="helloWorldServiceSoapBinding"  type ="ns1:HelloWorldServiceInf" >
  
< soap:binding  style ="document"  transport ="/soap/http"   />  
< wsdl:operation  name ="sayHello" >
  
< soap:operation  soapAction =""  style ="document"   />  
< wsdl:input  name ="sayHello" >
  
< soap:body  use ="literal"   />  
  
</ wsdl:input >
< wsdl:output  name ="sayHelloResponse" >
  
< soap:body  use ="literal"   />  
  
</ wsdl:output >
  
</ wsdl:operation >
  
</ wsdl:binding >
< wsdl:service  name ="helloWorldService" >
< wsdl:port  binding ="tns:helloWorldServiceSoapBinding"  name ="ServerPort" >
  
< soap:address  location ="http://localhost:9000/hello"   />  
  
</ wsdl:port >
  
</ wsdl:service >
  
</ wsdl:definitions >
客户端调用
package  ;

import  ;
import  ;
import  ;

import  ;

public   class  Client {
    
// 注意:此处http: // /  来源于wsdl文件中namespace   <wsdl:import location=" http://localhost :9000/hello?wsdl=" namespace=" / " /> 

    
private   static   final  QName SERVICE_NAME = new  QName( " / " , " HelloWorldServiceInf " ); // HelloWorldServiceInf接口类的名称
     private   static   final  QName PORT_NAME = new  QName( " / " " HelloWorldServiceInfPort " ); // HelloWorldServiceInfPort 接口类的名称+Port
     public   static   void  main(String[] args) {
        String endPointAddress
= " http://localhost:9000/hello " ;
        Service service
= (SERVICE_NAME);
        (PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
        HelloWorldServiceInf inf
= (HelloWorldServiceInf. class );
        ((
" 张三 " ));
    }
}
CXF根据wsdl文件动态调用WebService
package  ;

import  ;

public   class  ClientFromWsdl {
    
    
public   static   void  main(String[] args)  throws  Exception{
        JaxWsDynamicClientFactory dcf 
=  ();
         client 
=  ( " http://localhost:9000/hello?wsdl " );
        
// sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组
        Object[] objects = ( " sayHello " " 张三 " ); 
        
// 输出调用结果
        (objects[ 0 ].toString());
    }
}