Java - 使用从 WSDL 生成的 SOAP 类

Java - using SOAP with generated from WSDL classes

我有一个 WSDL 架构 link。使用 NetBeans,我从该模式生成了 classes。但是我不明白,如何使用它向服务器发送请求? NetBeans 生成了一个 XXXImplService extends Service class,我应该使用它吗?怎么样?

正如我所想,我只需要创建对象(匹配 WSDL 方法和 classes),设置必要的属性并以某种方式将此对象转换为请求文本,然后发送它使用并获取文本响应,我可以将其转换为 classes。这是真的吗?

您现在必须在此生成的服务实现和 Web 方法中实现您的代码。因此,当您将通过 Web 服务客户端(SOAP UI 等)调用服务端点和特定方法时,这些生成的 类 将通过服务实现调用和路由到您的实现.

当然您必须使用 WSDL,请按照以下步骤为 Java 网络服务 (JAX-WS) 创建完整的客户端应用程序:

假设您有这样的 Web 服务:

@WebService
public class Hello {
    private String message = new String("Hello, ");

    public void Hello() {}

    @WebMethod
    public String sayHello(String name) {
        return message + name + ".";
    }
}
  1. 使用javax.xml.ws.WebServiceRef 注解声明一个 对 Web 服务的引用。 @WebServiceRef 使用 wsdlLocation 用于指定已部署服务的 WSDL 文件的 URI 的元素:

    @WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/hello?wsdl") static HelloService service;

  2. 通过调用检索服务的代理,也称为端口 服务上的 getHelloPort。

    Hello port = service.getHelloPort(); 该端口实现了服务定义的 SEI。

  3. 调用端口的 sayHello 方法,向服务传递一个名称。

    String response = port.sayHello(name);
    

编辑:(在评论中请求)如果 Web 服务请求基本 身份验证 并且想要传递用户名和密码,您可以像这样传递它们(还有其他方法):

import java.net.Authenticator;
import java.net.PasswordAuthentication;

Authenticator authenticator = new Authenticator() 
{
    @Override
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication("usr", "pass".toCharArray());
    }
};

Authenticator.setDefault(authenticator );

however if you want authentication in application level not on basic HTTP this link can be useful.

本教程将帮助您一步步完成。由于您已经创建了存根 类,请跳过第一部分。关注 "Web service invocation" 部分。

http://www.ibm.com/developerworks/webservices/library/ws-apacheaxis/index.html?ca=dat