我在ubuntu下部署在jboss上的简单的“HelloWorld”Web服务.
我创建了简单的客户端,但我无法让它工作.我每次运行客户端时都会得到NullPointerException.
我创建了简单的客户端,但我无法让它工作.我每次运行客户端时都会得到NullPointerException.
请注意,我正在Ubuntu下运行Oracle Java 7.
这是代码:
HelloWorldClient.java
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- public class HelloWorldClient {
- public static void main(String[] args){
- URL url;
- try {
- url = new URL("http://localhost:8080/WebServiceProject/helloWorld?wsdl");
- QName qname = new QName("http:///","HelloWorldImplService");
- Service service = Service.create(url,qname);
- HelloWorld hello = service.getPort(HelloWorld.class);
- System.out.println(hello.sayHello("mkyong"));
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
}
HelloWorld.java
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- @WebMethod
- public String sayHello(String name);
- }
堆栈跟踪:
- Exception in thread "main" java.lang.NullPointerException
- at com.sun.xml.internal.ws.model.RuntimeModeler.getPortTypeName(RuntimeModeler.java:1407)
- at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:334)
- at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:354)
- at javax.xml.ws.Service.getPort(Service.java:188)
- at HelloWorldClient.main(HelloWorldClient.java:18)
在这一行抛出异常:
- HelloWorld hello = service.getPort(HelloWorld.class);
解决方法
我自己已经有同样的问题了几天,因为我使用的WSDL文件(和服务)被移动到一个新的URL.我终于在这里找到了解决方案:
http://techtracer.com/2007/08/15/jax-ws-jaxp-tutorial-building-a-stockquote-web-service-client/
总之,在使用以下命令(在Windows / CygWin上)重新生成所有自动生成的java和类文件之后,所有的东西(应该都有)开始工作了
- "C:/Program Files/Java/jdk1.8.0_31/bin/wsimport.exe" -keep https://domain.com/path_to_wsdl