我正在從CXF複製最簡單的Web服務示例;它通過編寫一個接口,然後是一個實現文件,來向網絡服務消費者提供的名稱打招呼。我改變了包名和方法名,因爲我想看看事情出現在哪裏;如果你命名一切HelloWorld你看不到什麼是方法,包,類等。WebService客戶端失敗:Tomcat,CXF
這些指令包括一個發佈web服務的程序。我這樣做,把URL
在瀏覽器中之後顯示包含足夠的東西,我拼寫它來說服我,這是從我的代碼生成方式的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在Window/Preferences中使用http和https的代理來設置;在運行客戶端之前,我刪除了一個http,但沒有更改該消息。
它實際上是一個tomcat服務器;我在發佈程序中嘗試了替代網址,但沒有任何更改。
在這種情況下,我不在eclipse中運行tomcat;我在我的機器上自己運行它,然後運行發佈程序(從eclipse),驗證顯示wsdl工作正常的URL,然後運行客戶端程序(從eclipse)並獲取我的錯誤。
有人能告訴我我做錯了什麼嗎?我已經看到了這個確切的錯誤消息的其他帖子,但沒有答案是明確的,我似乎已經嘗試了所有。
請參見本以下鏈接添加目標名稱 http://stackoverflow.com/a/15458568/993979 – 2013-03-17 07:41:22