今天学习了下cxf下面介绍下用cxf实现webService
1.首先从apache官网下载Apache-cxf
2.建立一个Java工程导入所需的包如下
servlet-api.jar 在tomcat下的lib包里面
3.现在新建一个接口HelloWord里面新建一个方法say
@WebService
public interface HelloWorld {
public String sayhi(String name);
}
记得类前面要用Webservice标注
4.新建一个实现类HelloWordImpl
@WebService(endpointInterface="org.Cherry.dao.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String sayhi(String name) {
System.out.println("Hello"+"\t"+name);
return "hello"+name;
}
}
5.新建一个服务类StartService
public class StartService {
protected StartService(){
HelloWorldImpl helloworld = new HelloWorldImpl();
String address = "http://localhost:8888/ns";
JaxWsServerFactoryBean server = new JaxWsServerFactoryBean();
server.setServiceClass(helloworld.getClass());
server.setAddress(address);
server.create();
}
public static void main(String[] args) {
new StartService();
}
}
6.新建一个客户端类 ClientTest
public class ClientTest {
public static void main(String args[]){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8888/ns");
HelloWorld client = factory.create(HelloWorld.class);
System.out.println("cxf");
client.sayhi("00");
}
}