java - WebService 客户端失败 : Tomcat, CXF

标签 java web-services cxf

我正在从 CXF 复制最简单的 Web 服务示例;它逐步编写一个接口(interface),然后是一个实现文件,以向 web 服务消费者提供的名称打招呼。我更改了一个包名和方法名,因为我想看看事情出现在哪里;如果您将所有内容命名为 HelloWorld,您将看不到什么是方法、包、类等。

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

http://localhost:9000/helloWorld?wsdl 

在浏览器中显示一个 wsdl 文件,其中包含我拼写的足够多的内容,使我相信它是从我的代码生成的。基于此,我假设 WSDL 生成和发布都有效。

这是服务接口(interface):

    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 有问题。

关于java - WebService 客户端失败 : Tomcat, CXF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455570/

相关文章:

java - 是否有 OSGi 的非 Java 替代品?

ajax - 如何使用 AngularJS 通过调用 Web 服务下载图像文件并保存到本地硬盘

java - 更换 Web 服务的最佳实践?

java - 输入流以-1开头

java - 使用 Apache CXF 通过 REST 解析 String[] 请求参数

Java拖动 Sprite 从头开始重绘

java - 波兰表示法的实现

java - 如何将数据从 Activity 发送到 java 类

web-services - 如何使用 hbase 处理 Web 请求

hibernate - 如何使用 JPA 持久保存 XMLGregorianCalendar?