Webservice实现

来源:互联网 发布:淘宝推荐系统 研究 编辑:程序博客网 时间:2024/05/21 17:11

 

 

使用JavaBean就可以生成,然后又wsdl文件。

有了wsdl文件可以生成客户端代码。

 

完成了webservice的实现。

 

 

现在使用开发工具开发实在是太快了。

 

  1. package com.rx;
  2. public class Calculate {
  3.     public int add(int a, int b) {
  4.         return a + b;
  5.     }
  6.     
  7.     public String sayHello(String name) {
  8.         
  9.         return "Hello : " + name + ", You are welcome !";
  10.     }
  11.     
  12. }

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <wsdl:definitions targetNamespace="http://rx.com" xmlns:impl="http://rx.com" xmlns:intf="http://rx.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsi="http://ws-i.org/profiles/basic/1.1/xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3.  <wsdl:types>
  4.   <schema targetNamespace="http://rx.com" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  5.    <element name="addResponse">
  6.     <complexType>
  7.      <sequence>
  8.       <element name="addReturn" type="xsd:int"/>
  9.      </sequence>
  10.     </complexType>
  11.    </element>
  12.    <element name="sayHello">
  13.     <complexType>
  14.      <sequence>
  15.       <element name="name" nillable="true" type="xsd:string"/>
  16.      </sequence>
  17.     </complexType>
  18.    </element>
  19.    <element name="sayHelloResponse">
  20.     <complexType>
  21.      <sequence>
  22.       <element name="sayHelloReturn" nillable="true" type="xsd:string"/>
  23.      </sequence>
  24.     </complexType>
  25.    </element>
  26.    <element name="add">
  27.     <complexType>
  28.      <sequence>
  29.       <element name="a" type="xsd:int"/>
  30.       <element name="b" type="xsd:int"/>
  31.      </sequence>
  32.     </complexType>
  33.    </element>
  34.   </schema>
  35.  </wsdl:types>
  36.    <wsdl:message name="addResponse">
  37.      <wsdl:part element="intf:addResponse" name="parameters"/>
  38.   </wsdl:message>
  39.    <wsdl:message name="addRequest">
  40.      <wsdl:part element="intf:add" name="parameters"/>
  41.   </wsdl:message>
  42.    <wsdl:message name="sayHelloResponse">
  43.      <wsdl:part element="intf:sayHelloResponse" name="parameters"/>
  44.   </wsdl:message>
  45.    <wsdl:message name="sayHelloRequest">
  46.      <wsdl:part element="intf:sayHello" name="parameters"/>
  47.   </wsdl:message>
  48.    <wsdl:portType name="Calculate">
  49.      <wsdl:operation name="add">
  50.        <wsdl:input message="intf:addRequest" name="addRequest"/>
  51.        <wsdl:output message="intf:addResponse" name="addResponse"/>
  52.     </wsdl:operation>
  53.      <wsdl:operation name="sayHello">
  54.        <wsdl:input message="intf:sayHelloRequest" name="sayHelloRequest"/>
  55.        <wsdl:output message="intf:sayHelloResponse" name="sayHelloResponse"/>
  56.     </wsdl:operation>
  57.   </wsdl:portType>
  58.    <wsdl:binding name="CalculateSoapBinding" type="intf:Calculate">
  59.  <wsaw:UsingAddressing wsdl:required="false" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"/>
  60.      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  61.      <wsdl:operation name="add">
  62.        <wsdlsoap:operation soapAction="add"/>
  63.        <wsdl:input name="addRequest">
  64.          <wsdlsoap:body use="literal"/>
  65.       </wsdl:input>
  66.        <wsdl:output name="addResponse">
  67.          <wsdlsoap:body use="literal"/>
  68.       </wsdl:output>
  69.     </wsdl:operation>
  70.      <wsdl:operation name="sayHello">
  71.        <wsdlsoap:operation soapAction="sayHello"/>
  72.        <wsdl:input name="sayHelloRequest">
  73.          <wsdlsoap:body use="literal"/>
  74.       </wsdl:input>
  75.        <wsdl:output name="sayHelloResponse">
  76.          <wsdlsoap:body use="literal"/>
  77.       </wsdl:output>
  78.     </wsdl:operation>
  79.   </wsdl:binding>
  80.    <wsdl:service name="CalculateService">
  81.      <wsdl:port binding="intf:CalculateSoapBinding" name="Calculate">
  82.        <wsdlsoap:address location="http://localhost:9080/WebServiceProject/services/Calculate"/>
  83.     </wsdl:port>
  84.   </wsdl:service>
  85. </wsdl:definitions>

下面的是IBM RAD生成的代理类,其他省了。。。。。。

 

 

 

 

    1. package com.rx;
    2. public class CalculateProxy implements com.rx.Calculate {
    3.   private boolean _useJNDI = true;
    4.   private String _endpoint = null;
    5.   private com.rx.Calculate __calculate = null;
    6.   
    7.   public CalculateProxy() {
    8.     _initCalculateProxy();
    9.   }
    10.   
    11.   private void _initCalculateProxy() {
    12.   
    13.   if (_useJNDI) {
    14.     try{
    15.       javax.naming.InitialContext ctx = new javax.naming.InitialContext();
    16.       __calculate = ((com.rx.CalculateService)ctx.lookup("java:comp/env/service/CalculateService")).getCalculate();
    17.       }
    18.     catch (javax.naming.NamingException namingException) {}
    19.     catch (javax.xml.rpc.ServiceException serviceException) {}
    20.   }
    21.   if (__calculate == null) {
    22.     try{
    23.       __calculate = (new com.rx.CalculateServiceLocator()).getCalculate();
    24.       }
    25.     catch (javax.xml.rpc.ServiceException serviceException) {}
    26.   }
    27.   if (__calculate != null) {
    28.     if (_endpoint != null)
    29.       ((javax.xml.rpc.Stub)__calculate)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
    30.     else
    31.       _endpoint = (String)((javax.xml.rpc.Stub)__calculate)._getProperty("javax.xml.rpc.service.endpoint.address");
    32.   }
    33.   
    34. }
    35. public void useJNDI(boolean useJNDI) {
    36.   _useJNDI = useJNDI;
    37.   __calculate = null;
    38.   
    39. }
    40. public String getEndpoint() {
    41.   return _endpoint;
    42. }
    43. public void setEndpoint(String endpoint) {
    44.   _endpoint = endpoint;
    45.   if (__calculate != null)
    46.     ((javax.xml.rpc.Stub)__calculate)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
    47.   
    48. }
    49. public com.rx.Calculate getCalculate() {
    50.   if (__calculate == null)
    51.     _initCalculateProxy();
    52.   return __calculate;
    53. }
    54. public int add(int a, int b) throws java.rmi.RemoteException{
    55.   if (__calculate == null)
    56.     _initCalculateProxy();
    57.   return __calculate.add(a, b);
    58. }
    59. public java.lang.String sayHello(java.lang.String name) throws java.rmi.RemoteException{
    60.   if (__calculate == null)
    61.     _initCalculateProxy();
    62.   return __calculate.sayHello(name);
    63. }
    64. }
    package com.rx;
  1. import java.rmi.RemoteException;
  2. public class Test {
  3.     
  4.     
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         Calculate calculateProxy = new CalculateProxy();
  10.         try {
  11.             calculateProxy.sayHello("china");
  12.         } catch (RemoteException e) {
  13.             e.printStackTrace();
  14.         }
  15.     }
  16. }

 

 

在JavaBean中调用webservice需要runtime下的2个包

默认路径: c:/parogram Files/IBM/SDP70/runtimes/base_v61/runtimes
com.ibm.ws.admin.client_6.1.0.jar
com.ibm.ws.webservices.thinclient_6.1.0.jar

原创粉丝点击