简体   繁体   English

WebService客户端失败:Tomcat,CXF

[英]WebService client fails: Tomcat, CXF

I am copying the simplest web service example from CXF; 我正在从CXF复制最简单的Web服务示例; it steps through writing an interface, then an implementation file, to say hello to a name provided by the webservice consumer. 它逐步编写一个接口,然后是一个实现文件,向webservice使用者提供的名称打招呼。 I changed a package name and the method name because I wanted to see where things showed up; 我改变了一个包名和方法名,因为我想知道出现的地方; if you name everything HelloWorld you can't see what is method, package, class, etc. 如果你命名一切HelloWorld,你就看不到什么是方法,包,类等。

Those instructions include a program to publish the web service. 这些说明包括发布Web服务的程序。 After I do that, putting the URL 我这样做后,把URL

http://localhost:9000/helloWorld?wsdl 

in a browser displays a wsdl file that contains enough stuff the way I spelled it to convince me that it was generated from my code. 在浏览器中显示一个wsdl文件,其中包含足够的东西,我拼写它以说服我它是从我的代码生成的。 I assume, based on this, that both the WSDL generation and the publication worked. 基于此,我假设WSDL生成和发布都有效。

This is the service interface: 这是服务接口:

    package hw;

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

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

This is the service implementation: 这是服务实现:

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."; 
    }
}

And this is the publishing program: 这是出版计划:

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);
    }
}

Now I compile and run this program: 现在我编译并运行这个程序:

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"));

    }

}

and I get this error: 我收到此错误:

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

I am running the programs -- both publish and client -- from eclipse. 我从eclipse运行程序 - 发布和客户端。 The eclipse is set up with proxies for http and https in Window / Preferences; eclipse在Window / Preferences中设置了http和https的代理; I removed the one for http before running the client, but it did not change the message. 我在运行客户端之前删除了一个http,但它没有更改消息。

It is in fact a tomcat server; 它实际上是一个tomcat服务器; I tried the alternate URL in the publish program with no change. 我在发布程序中尝试了备用URL而没有任何更改。

I don't run tomcat from within eclipse in this case; 在这种情况下,我不会在eclipse中运行tomcat; I run it by itself on my machine and then run the publish program (from eclipse), verify the url that displays the wsdl works correctly, and then run the client program (from eclipse) and get my error. 我在我的机器上单独运行它,然后运行发布程序(来自eclipse),验证显示wsdl正常工作的url,然后运行客户端程序(来自eclipse)并得到我的错误。

Can someone tell me what I'm doing wrong? 有人能告诉我我做错了什么吗? I've seen other posts on this exact error message, but none of the answers were definitive and I appear to have tried them all. 我已经在这个确切的错误消息上看过其他帖子,但没有一个答案是确定的,我似乎已经尝试了所有这些。

Not sure this is your problem. 不确定这是你的问题。

I've sometimes had problems with eclipse not being able to run tomcat applications on a running tomcat as you describe in your example. 我有时会遇到eclipse无法在运行的tomcat上运行tomcat应用程序的问题,正如您在示例中所描述的那样。 What I sometimes have to do when working with tomcat and eclipse is either 我在处理tomcat和eclipse时有时需要做的事情也是

  1. have a running tomcat (windows service) and then export my eclipse application to that tomcat 有一个运行的tomcat(Windows服务),然后将我的eclipse应用程序导出到该tomcat
  2. stop the running tomcat on that port from windows services and start the tomcat from inside eclipse when running the program. 从Windows服务停止该端口上运行的tomcat,并在运行程序时从eclipse内启动tomcat。

For some reason eclipse seems to have problems with an already running tomcat. 出于某种原因,eclipse似乎与已经运行的tomcat有问题。

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

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