3
votes

I am copying the simplest web service example from CXF; it steps through writing an interface, then an implementation file, to say hello to a name provided by the webservice consumer. 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.

Those instructions include a program to publish the web service. After I do that, putting the 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. I assume, based on this, that both the WSDL generation and the publication worked.

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. The eclipse is set up with proxies for http and https in Window / Preferences; I removed the one for http before running the client, but it did not change the message.

It is in fact a tomcat server; I tried the alternate URL in the publish program with no change.

I don't run tomcat from within eclipse in this case; 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.

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.

1
Please see this below link for adding targetnamespace stackoverflow.com/a/15458568/993979java specialist

1 Answers

0
votes

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. What I sometimes have to do when working with tomcat and eclipse is either

  1. have a running tomcat (windows service) and then export my eclipse application to that tomcat
  2. stop the running tomcat on that port from windows services and start the tomcat from inside eclipse when running the program.

For some reason eclipse seems to have problems with an already running tomcat.