cxf的例子

建一个java工程,工程名字为cxftest1,在包test下面建以下四个类
先运行服务器,然后运行客户端

HelloWorld.java 接口
package test;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    public String sayHello(String name);
}


HelloWorldImpl.java 实现类
package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {

    public String sayHello(String name) {
        System.out.println("sayHello");
        return "sayHello " + name;
    }

}


MainServer.java 服务器端
package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

    public static void main(String[] args) {
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloWorldImpl.class);
        factory.setAddress("http://localhost:8080/cxftest1");
        factory.getInInterceptors().add(new LoggingInInterceptor());
        Server server = factory.create();
        server.start();
    }

}


HelloWorldClient.java 客户端
package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloWorldClient {

    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8080/cxftest1/HelloWorld");
        factory.setServiceClass(HelloWorld.class);
        HelloWorld helloWorld = (HelloWorld) factory.create();
        System.out.println(helloWorld.sayHello("chinagggg"));
    }

}

你可能感兴趣的:(java,apache,webservice)