[color=brown]警告: Interceptor for {http://server.hw.demo/}HelloWorld#{http://client.webserver.com/}sayHi has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:48)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
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)
at $Proxy35.sayHi(Unknown Source)
at com.webserver.client.Client.main(Client.java:49)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:890)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:872)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:798)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:546)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
... 8 more
Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
at $Proxy35.sayHi(Unknown Source)
at com.webserver.client.Client.main(Client.java:49)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:890)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:872)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:798)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:546)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
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
[/color]
關於這個錯誤,本人被困惑了兩天,特別浪費了星期天保貴的休息日。剛才,認真下來分析,嘗試,鈡於被我蒙到了。
在查看網上的相關的信息後,一直以為QNAME與PORT_NAME是targetNamespace的值,並沒有想到是客戶端的接口類包名,如本例中的接口包名“com.webserver.client”.
(本人的服務端與客戶端分屬不同的項目)
所以,記住QNAME與POR_NAME是客戶端的接口類的包名倒寫。
WSDL信息
<wsdl:definitions name="HelloWorld" targetNamespace="http://server.hw.demo/"><wsdl:types><xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://server.hw.demo/"><xsd:element name="sayHi" type="tns:sayHi"/><xsd:complexType name="sayHi"><xsd:sequence><xsd:element minOccurs="0" name="arg0" type="xsd:string"/></xsd:sequence></xsd:complexType><xsd:element name="sayHiResponse" type="tns:sayHiResponse"/><xsd:complexType name="sayHiResponse"><xsd:sequence><xsd:element minOccurs="0" name="return" type="xsd:string"/>
、、、、、、、、、、
正常運行的代碼
package com.webserver.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://client.webserver.com/", "HelloWorld");
private static final QName PORT_NAME
= new QName("http://client.webserver.com/", "HelloWorldPort");
//正確的寫法 ,與本地HelloWorld.java接口包名關聯。
private Client() {
}
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:8080/first_jaxws/services/HelloWorld?wsdl";
// 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("World"));
/* User user = new UserImpl("World");
System.out.println(hw.sayHiToUser(user));
//say hi to some more users to fill up the map a bit
user = new UserImpl("Galaxy");
System.out.println(hw.sayHiToUser(user));
user = new UserImpl("Universe");
System.out.println(hw.sayHiToUser(user));
System.out.println();
System.out.println("Users: ");
Map<Integer, User> users = hw.getUsers();
for (Map.Entry<Integer, User> e : users.entrySet()) {
System.out.println(" " + e.getKey() + ": " + e.getValue().getName());
}
*/
}
}