设为首页 加入收藏

TOP

java调用webservice的各种方法总结(2)
2014-11-23 21:52:24 】 浏览:197
Tags:java 调用 webservice 各种 方法 总结

  三、使用axis1.4调用webservice方法


  前提条件:下载axis1.4包和tomcat服务器 ,并将axis文件夹复制到tomcat服务器的webapp文件夹中


  这里我就说一下最简单的方法:


  首先建立一个任意的java类(例如:HelloWorld.java),复制到axis文件夹下,将其扩展名改为jws,然后重新启动tomcat,在浏览器中输入http://localhost:8989/axis/HelloWorld.jws wsdl,就会得到一个wsdl文件,其客户端调用方法如下:


  Java代码


  import javax.xml.rpc.Service;


  import javax.xml.rpc.ServiceException;


  import javax.xml.rpc.ServiceFactory;


  import java.net.MalformedURLException;


  import java.net.URL;


  import java.rmi.RemoteException;


  import javax.xml.namespace.QName;


  public class TestHelloWorld {


  public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {


  // TODO Auto-generated method stub


  String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws wsdl";


  String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";


  String serviceName = "HelloWorldService";


  String portName = "HelloWorld";


  ServiceFactory serviceFactory = ServiceFactory.newInstance();


  Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));


  HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);


  System.out.println("return value is "+proxy.getName("john") ) ;


  }


  }


  import javax.xml.rpc.Service;


  import javax.xml.rpc.ServiceException;


  import javax.xml.rpc.ServiceFactory;


  import java.net.MalformedURLException;


  import java.net.URL;


  import java.rmi.RemoteException;


  import javax.xml.namespace.QName;


  public class TestHelloWorld {


  public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {


  // TODO Auto-generated method stub


  String wsdlUrl ="http://localhost:8989/axis/HelloWorld.jws wsdl";


  String nameSpaceUri ="http://localhost:8989/axis/HelloWorld.jws";


  String serviceName = "HelloWorldService";


  String portName = "HelloWorld";


  ServiceFactory serviceFactory = ServiceFactory.newInstance();


  Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));


  HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);


  System.out.println("return value is "+proxy.getName("john") ) ;


  }


  }


  四、使用axis2开发webservice


  使用axis2 需要先下载


  axis2-1.4.1-bin.zip


  axis2-1.4.1-war.zip


  http://ws.apache.org/axis2/


  同理,也需要将axis2复制到webapp目录中


  在axis2中部署webservice有两种方法,


  第一种是pojo方式,这种方式比较简单,但是有一些限制,例如部署的类不能加上包名


  第二种方式是利用xml发布webservice,这种方法比较灵活,不需要限制类的声明


  下面分别说明使用方法:


  1.pojo方式:在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。其中POJO中所有的public方法将被发布成WebService方法。先实现一个pojo类:


  Java代码


  public class HelloWorld{


  public String getName(String name)


  {


  return "你好 " + name;


  }


  public int add(int a,int b)


  {


  return a+b;


  }


  }


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java调用webservice的各种方法总.. 下一篇Java语言中容易被人忽视的细节

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目