java 使用 Axis2 创建 Web 服务的步骤 - 客户端代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2559074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:46:56  来源:igfitidea点击:

Steps in creating a web service using Axis2 - The client code

javaweb-servicesaxis2

提问by zengr

I am trying to create a web service, my tools of trade are:

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

**

**

Axis2, Eclipse, Tomcat, Ant

Axis2、Eclipse、Tomcat、Ant

**

**

I need to create a web service from Code, i.e. Write a basic java class which will have the methods to be declared in the WSDL. Then use java2WSDL.sh to create my WSDL.

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

So, is this approach correct:

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

  1. Write my Java class with actual business logic
  1. 用实际的业务逻辑编写我的 Java 类
package packageNamel;

public class Hello{
public void World(String name)
          {
            SOP("Hello" + name);
          }
}
package packageNamel;

public class Hello{
public void World(String name)
          {
            SOP("Hello" + name);
          }
}
  1. Now, when I pass this Hello.java to java2WSDL.sh, this will give me the WSDL.
  2. Finally, I will write the services.xml file, and create the Hello.aar with following dir structure:

    Hello.aar

    • packageName
      • Hello.class
    • META-INF
      • services.xml
      • MANIFEST.MF
      • Hello.WSDL
  1. 现在,当我将这个 Hello.java 传递给 java2WSDL.sh 时,这会给我 WSDL。
  2. 最后,我将编写 services.xml 文件,并使用以下目录结构创建 Hello.aar:

    你好.aar

    • 包裹名字
      • 同学们好
    • 元信息
      • 服务.xml
      • 清单文件
      • 你好.WSDL

Now, I assume, my service will be deployed when I put the aar in tomcat1/webapps/axis2/WEB-INF/services

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

But, here comes my problem, HOW DO I ACCESS THE METHOD World(String name)???!!, i.e. I am clueless about the client code!

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

Please enlighten me on making a very basic web service and calling the method. The above described 3 steps might be wrong. It's a community wiki, feel free to edit.

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

Thanks

谢谢

采纳答案by Mark O'Connor

I'm assuming you're only interested in web service clients?

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

Option 1

选项1

Invoke the web service is using Axis2 REST support, for example:

使用 Axis2 REST 支持调用 Web 服务,例如:

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

http://localhost:8080/axis2/services/MyService/myOperation?param1=one¶m2=two

Option 2

选项 2

Use SOAPUI. It can generate SOAP messages for you, by reading your service's WSDL. My client's testers have been using it extensively with only a very broad understanding of web service technologies. An impressive tool.

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

Option 3

选项 3

Groovy client (Same approach for other JVM based languages)

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

Use the wsdl2javatool to create a client stub class for the Shakespeare web service:

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

generate.sh:

生成.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:

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()

Use the -cp parameter to add the generated code the the script's classpath

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

groovy -cp build/build/classes GetSpeech

回答by siddhartha chakraborty

If you have access to the WSDL,the following code/JAX-WS clientcan be used to invoke any SOAP based web service.

如果您有权访问 WSDL,则以下代码/ JAX-WS 客户端可用于调用任何基于 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));

    }

}

In Line 2,QNamefirst argument is the namespace used in WSDL and second argument is simply the service name.

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