如何从java调用Web服务(由wsdl描述)

时间:2009-12-01 14:05:07

标签: java web-services soap

对网络服务一无所知,我只想尝试一下wsdl描述的“isAlive”服务。

在我看来,这应该只需要不超过2-5行代码,但我似乎找不到任何东西,只有大量长的例子涉及第三方软件包等。

任何人都有任何想法?如果它总是被认为是长的可能是一个很好的解释,为什么它必须如此复杂也将不胜感激。 我正在使用Eclipse,而wsdl是SOAP。

4 个答案:

答案 0 :(得分:6)

JDK 6附带了jax-ws,它是为Web服务开发客户端所需的一切。

我无法找到一些简单的示例来发布,但请从https://jax-ws.dev.java.net/

开始

编辑:这是一个简单的示例 - 此Web服务的客户端:http://xmethods.com/ve2/ViewListing.po?key=427565

C:\temp> md generated
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\wsimport -keep -d generated http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl

创建PrimeClient.java,如下所示:

import javax.xml.ws.WebServiceRef;
import com.microsoft.webservices.*; 
//the above namespace is from the generated code from the wsdl. 

public class PrimeClient {
 //Cant  get this to work.. @WebServiceRef(wsdlLocation="http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl")
  static PrimeNumbers service;

  public static void main(String[] args) {
    try {
    service = new PrimeNumbers();
      PrimeClient client = new PrimeClient();
      client.doTest(args);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void doTest(String[] args) {
    try {
      System.out.println("Retrieving the port from the following service: " + service);
      PrimeNumbersSoap pm = service.getPrimeNumbersSoap();
      System.out.println("Invoking the getPrimeNumbersSoap operation ");
      System.out.println(pm.getPrimeNumbers(100));
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
} 

编译并运行:

C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\javac -cp generated PrimeClient.java
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\java -cp .;generated PrimeClient
Retrieving the port from the following service: com.microsoft.webservices.PrimeN
umbers@19b5393
Invoking the getPrimeNumbersSoap operation
1,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97

答案 1 :(得分:4)

IDE的插件可生成所需的代码以便为您使用Web服务。

在插件生成基本方法之后,您只需调用这样的Web服务:

TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();

查看http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/

答案 2 :(得分:1)

There are three ways to write a web service client

  1. 动态代理
  2. 动态调用界面(DII)
  3. 应用程序客户端
  4. 动态代理客户端示例

    import java.net.URL;
    
    import javax.xml.rpc.Service;
    
    import javax.xml.rpc.JAXRPCException;
    
    import javax.xml.namespace.QName;
    
    import javax.xml.rpc.ServiceFactory;
    
    import dynamicproxy.HelloIF;
    
    public class HelloClient {
    
        public static void main(String[] args) {
            try {
    
                String UrlString = "Your WSDL URL";  // 
                String nameSpaceUri = "urn:Foo";
                String serviceName = "MyHelloService";
                String portName = "HelloIFPort";
    
                System.out.println("UrlString = " + UrlString);
                URL helloWsdlUrl = new URL(UrlString);
    
                ServiceFactory serviceFactory =
                    ServiceFactory.newInstance();
    
                Service helloService =
                    serviceFactory.createService(helloWsdlUrl, 
                    new QName(nameSpaceUri, serviceName));
    
                dynamicproxy.HelloIF myProxy = 
                    (dynamicproxy.HelloIF) 
                    helloService.getPort(
                    new QName(nameSpaceUri, portName), 
                    dynamicproxy.HelloIF.class); 
    
                System.out.println(myProxy.sayHello("Buzz"));
    
            } catch (Exception ex) {
                ex.printStackTrace();
            } 
        } 
    }  
    

    I hope , this would solve your question.

答案 3 :(得分:-2)

迄今为止我发现最容易使用的是Idea IntelliJ向导 - 使用Metro库 - 生成一个非常小的代码片段,可以与Java 6一起使用。

相关问题