【问题标题】:Steps in creating a web service using Axis2 - The client code使用 Axis2 创建 Web 服务的步骤 - 客户端代码
【发布时间】:2011-02-03 06:48:05
【问题描述】:

我正在尝试创建一个网络服务,我的交易工具是:

**

Axis2、Eclipse、Tomcat、Ant

**

我需要从 Code 创建一个 Web 服务,即编写一个基本的 java 类,该类将具有要在 WSDL 中声明的方法。然后使用 java2WSDL.sh 创建我的 WSDL。

那么,这种方法是否正确:

  1. 用实际的业务逻辑编写我的 Java 类
package packageNamel;

public class Hello{
public void World(String name)
          {
            SOP("Hello" + name);
          }
}
  1. 现在,当我将此 Hello.java 传递给 java2WSDL.sh 时,这将为我提供 WSDL。
  2. 最后,我将编写 services.xml 文件,并创建具有以下 dir 结构的 Hello.aar:

    你好.aar

    • 包名
      • Hello.class
    • 元信息
      • services.xml
      • 清单.MF
      • Hello.WSDL

现在,我假设,当我将 aar 放入 tomcat1/webapps/axis2/WEB-INF/services 时,我的服务将被部署

但是,我的问题来了,我如何访问方法World(String name)???!!,即我对客户端代码一无所知!

请赐教我制作一个非常基本的Web服务并调用该方法。上述 3 个步骤可能是错误的。这是一个社区 wiki,请随时编辑。

谢谢

【问题讨论】:

    标签: java web-services axis2


    【解决方案1】:

    我假设您只对 Web 服务客户端感兴趣?

    选项 1

    调用使用Axis2的web服务REST support,例如:

    http://localhost:8080/axis2/services/MyService/myOperation?param1=one&param2=two

    选项 2

    使用SOAPUI。它可以通过读取服务的 WSDL 为您生成 SOAP 消息。我客户的测试人员一直在广泛使用它,但对 Web 服务技术只有非常广泛的了解。令人印象深刻的工具。

    选项 3

    Groovy 客户端(其他基于 JVM 的语言的方法相同)

    使用 wsdl2java 工具为 Shakespeare Web 服务创建客户端存根类:

    generate.sh

    $AXIS2_HOME/bin/wsdl2java.sh -d adb -s -o build -uri http://www.xmlme.com/WSShakespeare.asmx?WSDL
    ant -file build/build.xml 
    

    GetSpeech.groovy

    // Dependencies
    // ============
    import com.xmlme.webservices.ShakespeareStub
    
    @Grapes([
        @Grab(group='org.apache.axis2', module='axis2-kernel', version='1.5.1'),
        @Grab(group='org.apache.axis2', module='axis2-adb', version='1.5.1'),
        @Grab(group='org.apache.axis2', module='axis2-transport-local', version='1.5.1'),
        @Grab(group='org.apache.axis2', module='axis2-transport-http', version='1.5.1'),
        @Grab(group='xerces', module='xercesImpl', version='2.6.2'),
        @GrabConfig(systemClassLoader=true)
    ])
    
    // Main program
    // ============
    def stub = new ShakespeareStub()
    
    // Request payload
    def request = new ShakespeareStub.GetSpeech()
    request.setRequest("Friends, romans, countrymen")
    
    // Send request
    response = stub.getSpeech(request)
    
    println response.getGetSpeechResult()
    

    使用 -cp 参数将生成的代码添加到脚本的类路径中

    groovy -cp build/build/classes GetSpeech
    

    【讨论】:

      【解决方案2】:

      如果您有权访问 WSDL,则可以使用以下代码/JAX-WS client 调用任何基于 SOAP 的 Web 服务。

      import java.net.URL;
      import javax.xml.namespace.QName;
      import javax.xml.ws.Service;
      
      public class WebserviceClient {
      
          public static void main(String[] args) throws Exception {
      
              URL url = new URL
                      ("http://localhost:9999/ws/additionService?wsdl");
      
              QName qname = new QName("http://test/", 
                      "AdditionServiceImplService");//Line 2
      
              Service service = Service.create(url, qname);
      
              AdditionService additionService = service
                      .getPort(AdditionService.class);
      
              System.out.println(additionService.add(1, 2));
      
          }
      
      }
      

      在第 2 行,QName 第一个参数是 WSDL 中使用的命名空间,第二个参数只是服务名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2014-01-18
        • 1970-01-01
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多