如何在Java中的WSDL URL上调用Web服务? [英] How to invoke web services on WSDL URL in Java?

查看:312
本文介绍了如何在Java中的WSDL URL上调用Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在正在构建的Java Web应用程序中调用一些Web服务方法.

I need to invoke some web service methods within a java web application that I'm building.

例如,每次用户注册时,我都希望通过Java在WSDL URL上调用newUser方法.我需要在请求中传递一些参数.

E.g each time a user signs up, I want to call the newUser method on a WSDL url via Java. I'd need to pass on some parameters with the request.

是否有任何内置的Java类或任何公开可用的类可以使此操作变得容易,即我只提供URL和参数,然后它执行请求并返回响应?

Is there any built in Java class, or any publicly available class, which can make this easy, i.e I just supply the URL and the parameters, and it performs the request and returns the response?

如果没有,在Java应用程序中的WSDL上调用Web服务的标准方法是什么?

If not, what is the standard way of invoking web services on WSDL in Java applications?

推荐答案

在已部署的WSDL URL上运行wsimport,您可以从JDK运行它:

Run wsimport on the deployed WSDL URL , you can run it from your JDK:

wsimport -p client -keep http://localhost:8080/calculator?wsdl

此步骤将生成并编译一些类.注意-keep开关,您需要它来保留生成的Java源文件.

This step will generates and compile some classes. Notice -keep switch, you need it to keep the generated Java source files.

Calculator.java-服务端点接口或SEI
CalculatorService-生成的服务,实例化

Calculator.java - Service Endpoint Interface or SEI
CalculatorService - Generated Service, instantiate it

public class MyClientServiceImpl {
    public static void main(String args[]){

    @Override
    public Integer add(int a , int b) {
       CalculatorService service = new CalculatorService();
       Calculator calculatorProxy = service.getCalculatorPort();            
        /**
         * Invoke the remote method
         */
        int result = calculatorProxy.add(10, 20);
        System.out.println("Sum of 10+20 = "+result);
    }
}

如果您使用的是受Java EE 6支持的容器,则可以这种方式使用它,

If you are using the Java EE 6 supported container then you can use it in this way ,

public class MyClientServiceImpl implements MyClientService {

    @WebServiceRef(wsdlLocation = "http://localhost:8080/calculator?wsdl", 
value = CalculatorService.class)
    private Calculator service;

    @Override
    public Integer add(int a , int b) {
        return service.add(a,b);
    }
}

这篇关于如何在Java中的WSDL URL上调用Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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