使用Java编译基本的Web服务客户端时出错

问题描述

我试图运行以下Java EE Web服务客户端几天:

package servclient;

import tuto.helloserv.HelloService;
import javax.xml.ws.WebServiceRef;

public class ServClient {

    @WebServiceRef(wsdlLocation = "http://localhost:1991/HelloService/HelloService?wsdl")
    public static HelloService helloServ;

    public static void main(String[] args) {
        System.out.println(webCall("world"));
    }

    private static String webCall(java.lang.String arg0) {
        tuto.helloserv.Hello port = helloServ.getHelloPort();
        return port.webCall(arg0);
    }
}

但是,即使Web服务url为true,我在netbeans上也会遇到以下错误

Exception in thread "main" java.lang.NullPointerException
    at servclient.ServClient.webCall(ServClient.java:25)
    at servclient.ServClient.main(ServClient.java:21)
C:\Users\DevShop\Documents\wapps\ServClient\nbproject\build-impl.xml:1329: The following error occurred while executing this line:
C:\Users\DevShop\Documents\wapps\ServClient\nbproject\build-impl.xml:981: Java returned: 1
BUILD Failed (total time: 1 second)

我在机器上运行Java 1.8.0_261,但在任何已知论坛中都找不到任何明确的解决方案。

解决方法

注释@WebServiceRef用于Java EE而不是SE

因此,要使用Java SE中的服务,您需要从wsdl中生成服务的客户端

请参阅有关wsimport的本教程:Java Brains wsimport tool 这也将有所帮助:Calling the service from generated client