论坛首页 Java企业应用论坛

CXF客户端Invalid address. Endpoint address cannot be null异常

浏览 5193 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-15  
1.服务器端代码
package com.sunyard.service.deploy;

import javax.xml.ws.Endpoint;

import com.sunyard.service.HelloWorldService;


/**
*
* @ClassName: DeployHelloWorldService
* @Description: TODO 发布CXF WEBSERVICE
* @Author:
* @Date: 2012-3-14 上午9:33:38 (创建文件的精确时间)
*/
public class DeployHelloWorldService {

/**
*
* @Title: deployService
* @Description: 发布WEBSERVICE
* @return void
*/
public static void deployService() {
System.out.println("Server Start ......");
HelloWorldService service = new HelloWorldService();
String address = "http://172.16.3.61:9000/helloWorld";
Endpoint.publish(address, service);
}

public static void main(String[] args) {
//发布webService
deployService();
System.out.println("Server ready ......");
try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("Server exiting ......");
//休眠60秒后退出
// System.exit(0);
}

}
package com.sunyard.service;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
HelloWorldService类代码
/**
*
* @ClassName: HelloWorldService
* @Description: TODO WEBSERVICE CXF 服务端示例
* @Author:
* @Date: 2012-3-14 上午9:27:13 (创建文件的精确时间)
*/
@WebService(serviceName="HelloWorldService")
@SOAPBinding(style = Style.RPC)
public class HelloWorldService {

public String sayHello(@WebParam(name = "name") String name) {
System.out.println(name);
return name + " say:  Hello World ";
}

}

2.客户端代码
package com.sunyard.client;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import com.sunyard.service.IHelloWorldService;


/**
*
* @ClassName: HelloWorldServiceClient
* @Description: TODO WEBSERVICE客户端调用代码
* @Author:
* @Date: 2012-3-14 上午10:19:40 (创建文件的精确时间)
*/
public class HelloWorldServiceClient {
private final static QName SERVICE_NAME = new QName("http://service.sunyard.com/", "HelloWorldService");
private final static QName PORT_NAME = new QName("http://service.sunyard.com/", "HelloWorldServicePort");
/**
* @param args
*/
public static void main(String[] args) {
Service service = Service.create(SERVICE_NAME);
String endpointAddress = "http://172.16.3.61:9000/helloWorld"; //wsdl地址
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
IHelloWorldService hw = service.getPort(IHelloWorldService.class); //发布服务的接口
System.out.println(hw.sayHello("name")); //接口方法调用
}
// public static void main(String[] args) {
// //调用webservice
// JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// factory.setServiceClass(IHelloWorldService.class);
// factory.setAddress("http://172.16.3.61:9000/helloWorld");
//
// IHelloWorldService service = (IHelloWorldService) factory.create();
// System.out.println("[result]" + service.sayHello("chain"));
// }
}
IHelloWorldService类代码
package com.sunyard.service;

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

/**
*
* @ClassName: IHelloWorldService
* @Description: TODO 客户端调用WEBSERVICE所需要的接口
* @Author:
* @Date: 2012-3-14 上午10:17:00 (创建文件的精确时间)
*/
@WebService
public interface IHelloWorldService {
@WebMethod
public String sayHello(@WebParam(name = "name") String name);
}
3.运行客户端后报错信息
警告: Interceptor for {http://service.sunyard.com/}HelloWorldService#{http://service.sunyard.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:263)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
at $Proxy22.sayHello(Unknown Source)
at com.sunyard.client.HelloWorldServiceClient.main(HelloWorldServiceClient.java:28)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:744)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:728)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:675)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:464)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
... 9 more
Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
at $Proxy22.sayHello(Unknown Source)
at com.sunyard.client.HelloWorldServiceClient.main(HelloWorldServiceClient.java:28)
Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:744)
at org.apache.cxf.transport.http.HTTPConduit.getURL(HTTPConduit.java:728)
at org.apache.cxf.transport.http.HTTPConduit.setupURL(HTTPConduit.java:675)
at org.apache.cxf.transport.http.HTTPConduit.prepare(HTTPConduit.java:464)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
... 2 more
4.补充:如果运行客户端中以下代码可以正常运行
public static void main(String[] args) {
//调用webservice
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IHelloWorldService.class);
factory.setAddress("http://172.16.3.61:9000/helloWorld");

IHelloWorldService service = (IHelloWorldService) factory.create();
System.out.println("[result]" + service.sayHello("chain"));
}
运行结果:
2012-4-15 0:28:28 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.sunyard.com/}IHelloWorldServiceService from class com.sunyard.service.IHelloWorldService
[result]chain say:  Hello World
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics