【问题标题】:java sample web service started with "java.lang.NullPointerException", how to fix?java 示例 Web 服务以“java.lang.NullPointerException”开始,如何修复?
【发布时间】:2018-12-27 10:43:41
【问题描述】:

我从网上得到了一个简单的 java web 服务示例程序:

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.Date;

@WebService
interface IService {
    void hello(@WebParam(name="username") String username);
}

@WebService(targetNamespace = "ServiceImpl", endpointInterface="IService")
class ServiceImp implements IService{
    @Override
    public void hello(@WebParam(name = "username") String username) {
        System.out.println("hello " + username + " now is " + new Date());
    }
}

public class ServiceMain {
    public static void main(String[] args) {
        String address = "http://localhost:7777/myService";
        Endpoint.publish(address, new ServiceImp());
        System.out.println("OK");
    }
}

它编译并运行异常:

Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1618)
at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1584)
at com.sun.xml.internal.ws.server.EndpointFactory.create(EndpointFactory.java:226)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:144)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:563)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:545)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:308)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:231)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:126)
at javax.xml.ws.Endpoint.publish(Endpoint.java:240)
at ServiceMain.main(ServiceMain.java:22)

那么这段代码 sn-p 哪里出错了,如何解决呢? 非常感谢。

【问题讨论】:

    标签: java web exception service nullpointerexception


    【解决方案1】:

    您必须为端点接口提供完全限定名称。如果您想丢失端点接口,请尝试此操作。

    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;
    import java.util.Date;
    
    
    interface IService {
        void hello(String username);
    }
    
    @WebService(targetNamespace = "ServiceImpl")
    class ServiceImp implements IService{
    
        public void hello(@WebParam(name = "username") String username) {
             System.out.println("hello " + username + " now is " + new Date());
        }
    }
    
    public class ServiceMain {
        public static void main(String[] args) {
            String address = "http://localhost:7777/myService";
            Endpoint.publish(address, new ServiceImp());
            System.out.println("OK");
        }
    }
    

    否则,假设您的端点接口位于名为 your.pkg 的包中,请尝试此操作。

    package your.pkg;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.xml.ws.Endpoint;
    import java.util.Date;
    
    @WebService
    interface IService {
        void hello(String username);
    }
    
    @WebService(targetNamespace = "ServiceImpl", endpointInterface="your.pkg.IService")
    class ServiceImp implements IService{
    
        public void hello(@WebParam(name = "username") String username) {
             System.out.println("hello " + username + " now is " + new Date());
        }
    }
    
    public class ServiceMain {
        public static void main(String[] args) {
            String address = "http://localhost:7777/myService";
            Endpoint.publish(address, new ServiceImp());
            System.out.println("OK");
        }
    }
    

    我能够使用这两种方法运行它并开始从端点获取 WSDL :- http://localhost:7777/myService?wsdl

    【讨论】:

    • 你做了什么?
    • 补充说明
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2010-10-05
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多