繁体   English   中英

WebService客户端失败:Tomcat,CXF

[英]WebService client fails: Tomcat, CXF

我正在从CXF复制最简单的Web服务示例; 它逐步编写一个接口,然后是一个实现文件,向webservice使用者提供的名称打招呼。 我改变了一个包名和方法名,因为我想知道出现的地方; 如果你命名一切HelloWorld,你就看不到什么是方法,包,类等。

这些说明包括发布Web服务的程序。 我这样做后,把URL

http://localhost:9000/helloWorld?wsdl 

在浏览器中显示一个wsdl文件,其中包含足够的东西,我拼写它以说服我它是从我的代码生成的。 基于此,我假设WSDL生成和发布都有效。

这是服务接口:

    package hw;

    import javax.jws.WebParam;
    import javax.jws.WebService;

    @WebService
    public interface HelloWorld
    {
        String sayHi(@WebParam(name="firstName") String firstName);
    }

这是服务实现:

package hwimpl;

import javax.jws.WebService;

@WebService(endpointInterface = "hw.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl
{
    static public void say(String msg) { System.out.println(msg); }

    public String sayHi(String firstName) 
    { say ("sayHi called with " + firstName); 
      return "Hello " + firstName + " from the World."; 
    }
}

这是出版计划:

package hwimpl;

import javax.xml.ws.Endpoint;

public class PublishHelloWorldService
{

    protected PublishHelloWorldService() throws Exception
    {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception
    {
        new PublishHelloWorldService();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

现在我编译并运行这个程序:

package client;

import hw.HelloWorld;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;


public final class HelloWorldClient
{

    private static final QName SERVICE_NAME = new QName("http://server.hw.demo/",   "HelloWorld");
    private static final QName PORT_NAME = new QName("http://server.hw.demo/",    "HelloWorldPort");

    private HelloWorldClient()
    {
    }

    public static void main(String args[]) throws Exception
    {
        Service service = Service.create(SERVICE_NAME);
        String endpointAddress = "http://localhost:9000/helloWorld";
        // If web service deployed on Tomcat deployment, endpoint should be changed
        // to:
        // String 
//      endpointAddress =
//       "http://localhost:8080/java_first_jaxws/services/hello_world";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

        HelloWorld hw = service.getPort(HelloWorld.class);
        System.out.println(hw.sayHi("Albert"));

    }

}

我收到此错误:

Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
    at com.sun.proxy.$Proxy20.sayHi(Unknown Source)
    at client.HelloWorldClient.main(HelloWorldClient.java:37)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
    at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:872)
    at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:854)
    at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:800)
    at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:548)
    at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)
    ... 2 more

我从eclipse运行程序 - 发布和客户端。 eclipse在Window / Preferences中设置了http和https的代理; 我在运行客户端之前删除了一个http,但它没有更改消息。

它实际上是一个tomcat服务器; 我在发布程序中尝试了备用URL而没有任何更改。

在这种情况下,我不会在eclipse中运行tomcat; 我在我的机器上单独运行它,然后运行发布程序(来自eclipse),验证显示wsdl正常工作的url,然后运行客户端程序(来自eclipse)并得到我的错误。

有人能告诉我我做错了什么吗? 我已经在这个确切的错误消息上看过其他帖子,但没有一个答案是确定的,我似乎已经尝试了所有这些。

不确定这是你的问题。

我有时会遇到eclipse无法在运行的tomcat上运行tomcat应用程序的问题,正如您在示例中所描述的那样。 我在处理tomcat和eclipse时有时需要做的事情也是

  1. 有一个运行的tomcat(Windows服务),然后将我的eclipse应用程序导出到该tomcat
  2. 从Windows服务停止该端口上运行的tomcat,并在运行程序时从eclipse内启动tomcat。

出于某种原因,eclipse似乎与已经运行的tomcat有问题。

暂无
暂无

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

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