首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从java调用web服务(由wsdl描述)

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

Stack Overflow用户
提问于 2009-12-01 22:05:07
回答 4查看 86.3K关注 0票数 24

对web服务一无所知,我只是尝试调用一些由wsdl描述的"isAlive“服务。

对我来说,这似乎只需要2-5行代码,但我似乎找不到任何东西,但我似乎找不到涉及第三方软件包等的巨大的长示例。

有谁有什么想法吗?如果它总是被认为是很长的,那么为什么它必须如此复杂,也许一个很好的解释也会受到赞赏。我使用的是Eclipse,而wsdl是SOAP。

EN

回答 4

Stack Overflow用户

发布于 2009-12-01 22:38:38

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

我找不到一些足够简单的例子来发布,但从https://jax-ws.dev.java.net/开始

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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();
    }
  }
} 

编译并运行:

代码语言:javascript
复制
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
票数 6
EN

Stack Overflow用户

发布于 2009-12-01 22:24:26

IDE提供了一些插件,可以生成所需的代码来为您使用web服务。

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

代码语言:javascript
复制
TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();

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

票数 4
EN

Stack Overflow用户

发布于 2009-12-01 23:05:54

There are three ways to write a web service client

(DII)

  • Application客户端动态调用接口
  1. Dynamic proxy

动态代理客户端的示例

代码语言:javascript
复制
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.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1826379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档