JAX-WS ::从独立的Java 7 SE客户端调用Web服务的方法 [英] JAX-WS :: ways to call a web service from a standalone Java 7 SE client

查看:303
本文介绍了JAX-WS ::从独立的Java 7 SE客户端调用Web服务的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试独立的JAX-WS Web服务,服务器和客户端(意思是,不在Java EE容器内运行)。一个好的SO帖子显示独立的服务器端是这一个

I am experimenting with standalone JAX-WS web services, server and client side (meaning, not running inside a Java EE container). A good SO post showing standalone server-side is this one.

对于客户端我发现以下三种方式似乎有效(使用 wsimport 生成客户端存根):

For the client side I've found the following three ways that seem to work (following use of wsimport to generate the client stubs):

public static void main(String[] args) throws Exception {
    String serviceURL = "http://localhost:9000/soap?wsdl";
    {   // WAY 1
        URL url = new URL(serviceURL);
        QName qname = new QName("urn:playground:jax-ws", "MyService");
        Service service = Service.create(url, qname);
        IHello port = service.getPort(IHello.class);
        System.out.println(port.sayHello("Long John"));
    }
    {   // WAY 2
        MyService service = new MyService();
        IHello port = service.getHelloPort();

        ((javax.xml.ws.BindingProvider) port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);

        System.out.println(port.sayHello("Long John"));
    }
    {   // WAY 3
        URL url = new URL(serviceURL);
        QName qname = new QName("urn:playground:jax-ws", "MyService");
        MyService service = new MyService(url, qname);
        IHello port = service.getHelloPort();
        System.out.println(port.sayHello("Long John"));
    }
}

我不知道任何其他客户模式 - 边访问或上面显示的方式如何相互比较。

I am not aware of any other patterns of client-side access or how the ways shown above compare against each other.

应该注意的任何其他方法或权衡?

Any other methods or trade-offs one should be aware of?

推荐答案

最后,经过一些实验,我认为下面显示的方式(摘自这里)与我之前的三个问题相比具有明显的优势:

In the end, after some experimentation, I think the way shown below (taken from here) has distinct advantages compared to the previous three in my question:

{   // WAY 4
    QName qname = new QName("urn:playground:jax-ws", "MyService");
    MyService service = new MyService(null, qname);
    IHello port = service.getHelloPort();
    BindingProvider bindingProvider = (BindingProvider) port;
    bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
    System.out.println(port.sayHello("John Silver"));
}

优点是:


  • WSDL在运行时检索(为什么它应该是?它已经在代码创建时用于创建客户端存根)

  • 服务的网址是 在存根中硬编码。

  • the WSDL is not retrieved at runtime (and why should it be? it's already been used at code creation time to create the client stub)
  • the URL of the service is not hardcoded in the stub.

这篇关于JAX-WS ::从独立的Java 7 SE客户端调用Web服务的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆