1、首先去官网下载cxf包
http://archive.apache.org/dist/cxf/
记住要选.zip结尾 大概40兆的样子
2、把上边的包都放项目里。如果你用的jeecg框架,那它自带,不过少了一个jetty的包。记得在pom.xml中加入
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
3、http://blog.csdn.net/oneGeng/article/details/5979442 这里面讲的很详细
第一步:新建一个webservice接口
- @WebService
- public interface IHelloWorld {
- //@WebParam给参数命名,提高可代码可读性。此项可选
- blic String sayHi(@WebParam(name="text") String text);
- }
通过注解@WebService申明为webservice接口
第二步,实现WebService接口
- @WebService
- public class HelloWorldImpl implements IHelloWorld {
- public String sayHi(String name) {
- System.out.println("sayHello is called by " + name);
- return "Hello " + name;
- }
- }
第三步,创建服务端
- public class Server {
- private Server(){
- IHelloWorld helloWorld = new HelloWorldImpl();
- //创建WebService服务工厂
- JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
- //注册WebService接口
- factory.setServiceClass(IHelloWorld.class);
- //发布接口
- factory.setAddress("http://localhost:9000/HelloWorld");
- factory.setServiceBean(helloWorld);
- //创建WebService
- factory.create();
- };
- public static void main(String[] args) throws InterruptedException{
- //启动服务端
- new Server();
- System.out.println("Server ready...");
- //休眠一分钟,便于测试
- Thread.sleep(1000*60);
- System.out.println("Server exit...");
- System.exit(0);
- }
- }
第四步,创建客户端
- public class Client {
- private Client(){};
- public static void main(String[] args){
- //创建WebService客户端代理工厂
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- //注册WebService接口
- factory.setServiceClass(HelloWorld.class);
- //设置WebService地址
- factory.setAddress("http://localhost:9000/HelloWorld");
- IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
- System.out.println("invoke webservice...");
- System.out.println("message context is:"+iHelloWorld.sayHi("
- Josen"));
- System.exit(0);
- }
- }
4、 首先,运行服务端程序
其次,打开浏览器,在地址栏中输入http://localhost:9000/HelloWorld?wsdl
5、当你运行服务端时,其他原有的程序报错了。比如:javax.servlet-api 读不到了 ,读了geronimo-servlet包中的javax-servlet。。
在pom.xml里换个位置就行了。程序喜欢读前边的那个。(纠结好久TT)