webservice复习cxf

webservice复习cxf
demo用的是cxf 3.0
接口:
 1 package  com;
 2
 3 import  javax.jws.WebParam;
 4 import  javax.jws.WebService;
 5
 6 @WebService
 7 public   interface  HelloWorldServiceInf
 8 {
 9    public String sayHello(@WebParam(name = "username")
10    String username);
11}


2. 实现类
package  com;

import  javax.jws.WebService;

import  org.apache.cxf.interceptor.LoggingInInterceptor;
import  org.apache.cxf.interceptor.LoggingOutInterceptor;
import  org.apache.cxf.jaxws.JaxWsServerFactoryBean;


@WebService(endpointInterface
= " com.HelloWorldServiceInf " ,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();
        factoryBean.setAddress(
"http://localhost:9000/hello");
        factoryBean.setServiceClass(HelloWorldServiceInf.
class);
        factoryBean.setServiceBean(impl);
        factoryBean.getInInterceptors().add(
new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(
new LoggingOutInterceptor());
        factoryBean.create();
    }

    
}



3. 客户端调用
 1 import  org.apache.cxf.endpoint.Client;
 2 import  org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 3
 4 public   class  ClientX
 5 {   
 6    public static void main(String[] args)throws Exception 
 7    {
 8        String wsdlUrl = "http://localhost:9000/hello?wsdl";
 9        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
10        Client client= factory.createClient(wsdlUrl);
11        Object[] res = client.invoke("sayHello""hwp");
12        System.out.println(res[0]);
13    }

14
15}

package  com.cxf.client;

import  org.apache.cxf.interceptor.LoggingInInterceptor;
import  org.apache.cxf.interceptor.LoggingOutInterceptor;
import  org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import  com.cxf.interfaces.HelloWorldServiceInf;

public   class  Client  {
    
public static void main(String[] args) {
        JaxWsProxyFactoryBean  factoryBean
=new JaxWsProxyFactoryBean();
        factoryBean.getInInterceptors().add(
new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(
new LoggingOutInterceptor());
        factoryBean.setServiceClass(HelloWorldServiceInf.
class);
        factoryBean.setAddress(
"http://localhost:9000/hello");
        HelloWorldServiceInf impl
=(HelloWorldServiceInf) factoryBean.create();
        System.out.println(impl.sayHello(
"张三"));
    }

}



你可能感兴趣的:(webservice复习cxf)