首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

CXF测试报错(java.net.MalformedURLException: Invalid address. Endpoint address canno)

2012-02-28 
CXF测试报错(java.net.MalformedURLException: Invalid address. Endpoint address canno)警告: Intercept

CXF测试报错(java.net.MalformedURLException: Invalid address. Endpoint address canno)
警告: Interceptor for {http://iface.service.cxf.com/}Helloworld#{http://iface.service.cxf.com/}sayHello 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: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)
at $Proxy33.sayHello(Unknown Source)
at com.cxf.client.HelloWorldClient.main(HelloWorldClient.java:25)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:871)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:853)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:799)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:547)
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 $Proxy33.sayHello(Unknown Source)
at com.cxf.client.HelloWorldClient.main(HelloWorldClient.java:25)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:871)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:853)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:799)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:547)
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
代码如下:
接口HelloWorld:

Java code
package com.cxf.service.iface;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloWorld {    public String sayHello(@WebParam(name="text") String text);  }

实现HelloWorldImpl:
Java code
package com.cxf.service.impl;import javax.jws.WebService;import com.cxf.service.iface.HelloWorld;@WebService(serviceName="Helloworld")public class HelloWorldImpl implements HelloWorld {    public String sayHello(String text) {        return "hello " + text;    }}

服务端HelloWorldService:
Java code
package com.cxf.service.app;import javax.xml.ws.Endpoint;import com.cxf.service.iface.HelloWorld;import com.cxf.service.impl.HelloWorldImpl;public class HelloWorldService {    /**     * @param args     */    public static void main(String[] args) throws Exception {        HelloWorld hello = new HelloWorldImpl();        String address = "http://localhost:8080/MyService";        Endpoint.publish(address, hello);        System.out.println("服务已发布......");        }} 


客户端HelloWorldClient:
Java code
package com.cxf.client;import javax.xml.namespace.QName;import javax.xml.ws.Service;import javax.xml.ws.soap.SOAPBinding;import com.cxf.service.iface.HelloWorld;public class HelloWorldClient {//    private static String namespaceURI = "http://iface.service.cxf.com/";//    private static QName SERVICE_NAME = new QName(namespaceURI, "Helloworld");//    private static QName PORT_NAME = new QName(namespaceURI,"HelloworldPort");    //private static String namespaceURI = "http://iface.service.cxf.com/";       private static QName SERVICE_NAME = new QName("http://iface.service.cxf.com/", "Helloworld");      private static QName PORT_NAME = new QName("http://iface.service.cxf.com/","Helloworldport");         /**     * @param args     */    public static void main(String[] args) {        Service service = Service.create(SERVICE_NAME);          String endpointAddress = "http://localhost:8080/MyService";          service.addPort(PORT_NAME, SOAPBinding.SOAP12HTTP_BINDING, endpointAddress);          HelloWorld hw = service.getPort(HelloWorld.class);          String info = hw.sayHello("测试");          System.out.println(info);      }}

这个错到底是怎么回事?网上有说是QName那url的反斜杠没写(http://iface.service.cxf.com/),但是我写了的(经测试写和不写都报这个错),望大家指教!

[解决办法]
private static QName PORT_NAME = new QName("http://iface.service.cxf.com/","Helloworldport"); 

把 Helloworldport 改成 HelloworldPort 再试试看。

这个 Port 的名字可以通过 http://localhost:8080/MyService?wsdl 找到

热点排行