服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用jwsdp完成web service在java的入门(四)


  其中的各个任务都有说明.
  
  注意,如果你使用jax-rpc1.1.2的话,wsdeploy生成web.xml文件有些小错误,需要手工更改,这个问题在1.1.3中已经修正.
  
  好了,在eclipse中运行各个任务,把最后得到的skysoft.war,作为一个新的应用发布,也可发布到你现有的应用,需要解压这个文件,然后拷贝需要的文件以及库文件到你的应用中即可.
  
  (四) 调用服务
  
  调用服务的方式有三种:静态调用,动态调用,动态调用接口.
  
  1 静态调用,使用了wscompile生成的静态桩文件.
  
  package hello;
  
  import javax.xml.rpc.stub;
  
  import staticstub.*;
  
  public class helloclient {
  
  private string endpointaddress;
  
  public static void main(string[] args) {
  
  args=new string[]{"http://localhost:8080/skysoft/hello?wsdl"};
  
  system.out.println("endpoint address = " + args[0]);
  
  try {
  
  stub stub = createproxy();
  
  stub._setproperty(javax.xml.rpc.stub.endpoint_address_property,
  
  args[0]);
  
  helloif hello = (helloif) stub;
  
  system.out.println(hello.sayhello("duke!"));
  
  } catch (exception ex) {
  
  ex.printstacktrace();
  
  }
  
  }
  
  private static stub createproxy() {
  
  // note: myhelloservice_impl is implementation-specific.
  
  return (stub) (new myhelloservice_impl().gethelloifport());
  
  }
  
  }
  
  2 动态调用,主要是使用servicefactory来执行调用.
  
  package hello;
  
  import java.net.url;
  
  import javax.xml.rpc.service;
  
  import javax.xml.rpc.jaxrpcexception;
  
  import javax.xml.namespace.qname;
  
  import javax.xml.rpc.servicefactory;
  
  //import dynamicproxy.helloif;
  
  import staticstub.helloif;
  
  public class dynclient {
  
  public static void main(string[] args) {
  
  try {
  
  args = new string[] { "http://localhost:8080/skysoft/hello" };
  
  
  string urlstring = args[0] + "?wsdl";
  
  string namespaceuri = "urn:foo";
  
  string servicename = "myhelloservice";
  
  string portname = "helloifport";
  
  system.out.println("urlstring = " + urlstring);
  
  url hellowsdlurl = new url(urlstring);
  
  servicefactory servicefactory = servicefactory.newinstance();
  
  service helloservice = servicefactory.createservice(hellowsdlurl,
  
  new qname(namespaceuri, servicename));
  
  helloif myproxy = (helloif) helloservice.getport(new qname(
  
  namespaceuri, portname), helloif.class);
  
  system.out.println(myproxy.sayhello("buzz"));
  
  } catch (exception ex) {
  
  ex.printstacktrace();
  
  }
  
  }
  
  }
  
  3 动态调用接口(dii),这种方式不需要任何附加代码,根据wsdl提供的调用方法的元描述,动态确定方法及其参数返回类型,有些类似于corba的接口池提供的服务.
  
  package hello;
  
  import javax.xml.rpc.call;
  
  import javax.xml.rpc.service;
  
  import javax.xml.rpc.jaxrpcexception;
  
  import javax.xml.namespace.qname;
  
  import javax.xml.rpc.servicefactory;
  
  import javax.xml.rpc.parametermode;
  
  public class diiclient {
  
  private static string qnameservice = "myhelloservice";
  
  private static string qnameport = "helloif";
  
  private static string body_namespace_value = "urn:foo";
  
  private static string encoding_style_property = "javax.xml.rpc.encodingstyle.namespace.uri";
  
  private static string ns_xsd = "http://www.w3.org/2001/xmlschema";
  
  private static string uri_encoding = "http://schemas.xmlsoap.org/soap/encoding/";
  
  public static void main(string[] args) {
  
  args=new string[]{"http://localhost:8080/skysoft/hello?wsdl"};
  
  system.out.println("endpoint address dii = " + args[0]);
  
  try {
  
  servicefactory factory = servicefactory.newinstance();
  
  service service = factory.createservice(new qname(qnameservice));
  
  qname port = new qname(qnameport);
  
  call call = service.createcall(port);
  
  call.settargetendpointaddress(args[0]);
  
  call.setproperty(call.soapaction_use_property, new boolean(true));
  
  call.setproperty(call.soapaction_uri_property, "");
  
  call.setproperty(encoding_style_property, uri_encoding);
  
  qname qname_type_string = new qname(ns_xsd, "string");
  
  call.setreturntype(qname_type_string);
  
  call.setoperationname(new qname(body_namespace_value, "sayhello"));
  
  call.addparameter("string_1", qname_type_string, parametermode.in);
  
  string[] params = { "murph!" };
  
  string result = (string) call.invoke(params);
  
  system.out.println(result);
  
  
  } catch (exception ex) {
  
  ex.printstacktrace();
  
  }
  
  }
  
  }
  
  到此,这个web service入门描述就结束了,它提供了ws的sun描述,以及在tomcat上发布服务的过程和一个构建脚本,借助jsp插件,使用eclipse完成了一个简单的web服务开发,发布,并用三种方式进行了测试.

扫描关注微信公众号