简体   繁体   English

如何在Java中调用WSDL URL上的Web服务?

[英]How to invoke web services on WSDL URL in Java?

I need to invoke some web service methods within a java web application that I'm building. 我需要在我正在构建的java Web应用程序中调用一些Web服务方法。

Eg each time a user signs up, I want to call the newUser method on a WSDL url via Java. 例如,每次用户注册时,我都希望通过Java在WSDL URL上调用newUser方法。 I'd need to pass on some parameters with the request. 我需要传递一些带有请求的参数。

Is there any built in Java class, or any publicly available class, which can make this easy, ie I just supply the URL and the parameters, and it performs the request and returns the response? 是否有任何内置的Java类或任何公开可用的类,这可以使这很容易,即我只提供URL和参数,它执行请求并返回响应?

If not, what is the standard way of invoking web services on WSDL in Java applications? 如果没有,在Java应用程序中调用WSDL上的Web服务的标准方法是什么?

Run wsimport on the deployed WSDL URL , you can run it from your JDK: 在已部署的WSDL URL上运行wsimport,您可以从JDK运行它:

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

This step will generates and compile some classes. 此步骤将生成并编译一些类。 Notice -keep switch, you need it to keep the generated Java source files. 注意-keep switch,你需要它来保存生成的Java源文件。

Calculator.java - Service Endpoint Interface or SEI Calculator.java - 服务端点接口或SEI
CalculatorService - Generated Service, instantiate it CalculatorService - 生成的服务,实例化它

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);
    }
}

If you are using the Java EE 6 supported container then you can use it in this way , 如果您使用的是Java EE 6支持的容器,那么您可以这样使用它,

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM