简体   繁体   English

Web 服务客户端从 getMyWSPort 获取 NullPointerException

[英]Web service client getting NullPointerException from getMyWSPort

I have created a simple web service called TimeServerBean .我创建了一个名为TimeServerBean的简单 Web 服务。 It's working properly, the GlassFish server is running and I can access the WSDL file from browser.它工作正常,GlassFish 服务器正在运行,我可以从浏览器访问 WSDL 文件。 Note this is done on local host.请注意,这是在本地主机上完成的。

Next I created a new project and made a web service client and provided the URL to the WSDL file.接下来,我创建了一个新项目并创建了一个 Web 服务客户端并提供了 WSDL 文件的 URL。 Then I got some classes generated (JAX-WS).然后我生成了一些类(JAX-WS)。 On my client class I have this code:在我的客户端类上,我有以下代码:

public class SimpleClient {

    @WebServiceRef(wsdlLocation = "wsdl url here")
    static TimeServerBean_Service service;
    private TimeServerBean bean;

    public SimpleClient() {
         bean = service.getTimeServerBeanPort();
    }
    //methods here
}

Although I get null when I call the getTimeServerBeanPort .虽然我在调用getTimeServerBeanPort时得到 null 。 During that time the server is up and running.在此期间,服务器启动并运行。 Is there some obvious mistake?有什么明显的错误吗? TimeServerBean and TimeServerBean_Service are generated classes from the WSDL. TimeServerBeanTimeServerBean_Service是从 WSDL 生成的类。

Two suggestions:两个建议:

  1. DEFINITELY put your method in a try / catch block绝对将您的方法放在try / catch块中

  2. Assuming that service itself is null, then try doing an explicit service.create() instead of using the @WebServiceRef annotation.假设service本身为空,然后尝试执行显式service.create()而不是使用@WebServiceRef注释。 Here's a good example (Websphere, but same principle):这是一个很好的例子(Websphere,但原理相同):

http://www-01.ibm.com/support/docview.wss?uid=swg21264135 http://www-01.ibm.com/support/docview.wss?uid=swg21264135

The @WebServiceRef annotation is only supported in certain class types. @WebServiceRef 注释仅在某些类类型中受支持。 Examples are JAX-WS endpoint implementation classes, JAX-WS handler classes, Enterprise JavaBeans classes, and servlet classes.示例有 JAX-WS 端点实现类、JAX-WS 处理程序类、Enterprise JavaBeans 类和 servlet 类。 This annotation is supported in the same class types as the @Resource annotation.在与@Resource 注释相同的类类型中支持此注释。 See the Java Platform, Enterprise Edition (Java EE) 5 specification for a complete list of supported class types.有关受支持的类类型的完整列表,请参阅 Java Platform, Enterprise Edition (Java EE) 5 规范。

I generally do it by creating an instance using the interface and the class.我通常通过使用接口和类创建实例来实现。

public class SimpleClient {

    // interface TimeServerBean_Service class TimeServerBean
    @WebServiceRef(wsdlLocation = "wsdl url here")
    static TimeServerBean_Service port = new TimeServerBean.getTimeServerBeanPort();

    public static void main(String[] args) {
        try {
            System.out.println(port);
            System.out.println(port.methodWS("args"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //methods here
}

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

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