java调用webservice方法总结一
来源:优易学  2009-12-4 10:09:47   【优易学:中国教育考试门户网】   资料下载   IT书店
文章页内部300*250广告位
 一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务

  1.首先建立一个Web services EndPoint:

  Java代码

  package Hello;

  import javax.jws.WebService;

  import javax.jws.WebMethod;

  import javax.xml.ws.Endpoint;

  @WebService

  public class Hello {

  @WebMethod

  public String hello(String name) {

  return "Hello, " + name + "\n";

  }

  public static void main(String[] args) {

  // create and publish an endpoint

  Hello hello = new Hello();

  Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);

  }

  }

  package Hello;

  import javax.jws.WebService;

  import javax.jws.WebMethod;

  import javax.xml.ws.Endpoint;

  @WebService

  public class Hello {

  @WebMethod

  public String hello(String name) {

  return "Hello, " + name + "\n";

  }

  public static void main(String[] args) {

  // create and publish an endpoint

  Hello hello = new Hello();

  Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);

  }

  }

  2.使用 apt 编译 Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录

  3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示

  4.使用wsimport 生成客户端

  使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl

  5.客户端程序:

  Java代码

  class HelloClient{

  public static void main(String args[]) {

  HelloService service = new HelloService();

  Hello helloProxy = service.getHelloPort();

  String hello = helloProxy.hello("你好");

  System.out.println(hello);

  }

  }

  class HelloClient{

  public static void main(String args[]) {

  HelloService service = new HelloService();

  Hello helloProxy = service.getHelloPort();

  String hello = helloProxy.hello("你好");

  System.out.println(hello);

  }

  }

  二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的

  利用xfire开发WebService,可以有三种方法:

  1一种是从javabean 中生成;

  2 一种是从wsdl文件中生成;

  3 还有一种是自己建立webservice

  步骤如下:

  用myeclipse建立webservice工程,目录结构如下:

  首先建立webservice接口,

  代码如下:

  Java代码

  package com.myeclipse.wsExample;

  //Generated by MyEclipse

  public interface IHelloWorldService {

  public String example(String message);

  }

  package com.myeclipse.wsExample;

  //Generated by MyEclipse

  public interface IHelloWorldService {

  public String example(String message);

  }

  Java代码

  package com.myeclipse.wsExample;

  //Generated by MyEclipse

  public class HelloWorldServiceImpl implements IHelloWorldService {

  public String example(String message) {

  return message;

  }

  }

  package com.myeclipse.wsExample;

  //Generated by MyEclipse

  public class HelloWorldServiceImpl implements IHelloWorldService {

  public String example(String message) {

  return message;

  }

  }

  修改service.xml 文件,加入以下代码:

  Xml代码

  <service>

  <name>HelloWorldService</name>

  <serviceClass>

  com.myeclipse.wsExample.IHelloWorldService

  </serviceClass>

  <implementationClass>

  com.myeclipse.wsExample.HelloWorldServiceImpl

  </implementationClass>

  <style>wrapped</style>

  <use>literal</use>

  <scope>application</scope>

  </service>

  <service>

  <name>HelloWorldService</name>

  <serviceClass>

  com.myeclipse.wsExample.IHelloWorldService

  </serviceClass>

  <implementationClass>

[1] [2] 下一页

责任编辑:小草

收藏此页】【 】【打印】【回到顶部
等级考试课程列表页595*300
文章搜索:
 相关文章
计算机底部580*90广告
文章页右侧第一330*280广告
计算机文章页资讯推荐
热点资讯
文章页330尺寸谷歌广告位
资讯快报
热门课程培训