1. 客户端编写
package com.smt.service.test;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.smt.service.IBankingService;
/**********************************************************
*
* 功能描述: </br></br>
*
* 作者: chenab </br></br>
*
* 创建时间: 2011-7-14 下午05:24:47</br></br>
*
* 版本号:V1.0 </br></br>
**********************************************************/
public
class clientXFire {
private
static
final String
urls="http://192.168.7.177:8088/testXFire/services/BankingService";
private
void testService(){
try {
Service serviceModel =
new ObjectServiceFactory().create(IBankingService.
class);
serviceModel =
new ObjectServiceFactory().create(IBankingService.
class);
XFire xFire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory =
new XFireProxyFactory(xFire);
IBankingService service = (IBankingService)factory.create(serviceModel,
urls);
service.transferFunds("你好");
}
catch (MalformedURLException e) {
System.
out.println("错误!");
e.printStackTrace();
}
}
public
static
void main(String[] args)
throws MalformedURLException {
clientXFire c =
new clientXFire();
c.testService();
}
}
|
package com.smt;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
import org.w3c.dom.Document;
/**********************************************************
*
* 功能描述: </br></br>
*
* 作者:
chenab </br></br>
*
* 创建时间:
2011-7-16 下午05:14:25</br></br>
*
* 版本号:V1.0 </br></br>
**********************************************************/
public
class XFireClient {
private
static
final String
urls=" http://192.168.0.3:3001/smart_webservice/services/SmtDocService?wsdl
";
public
static
void main(String[] args)
throws MalformedURLException, Exception {
Client client =
new Client(
new URL(
urls));
Object[] xml = client.invoke("findSrcDoc",new Object[]{1L});
Document doc = (Document)xml[0];
System.
out.println(doc.getDocumentElement().getTextContent());
}
}
|
package com.smt.service.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**********************************************************
*
* 功能描述: </br></br>
*
* 作者: chenab </br></br>
*
* 创建时间: 2011-7-14 下午05:48:31</br></br>
*
* 版本号:V1.0 </br></br>
**********************************************************/
public
class clientAxis {
private
static
final String
urls="http://192.168.7.177:8088/testXFire/services/BankingService";
public
static
void main(String[] args) {
try {
/*
* Banking是webservice在services.xml注册的名字
*/
Service service =
new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(
new java.net.URL(
urls));
// 由于需要认证,故需要设置调用的用户名和密码。
// call.getMessageContext().setUsername("sophia");
// call.getMessageContext().setPassword("sophia");
/*
* 调用service的方法名
*/
call.setOperationName("transferFunds");
/*
* invoke方法的参数是Object数组,该数组元素个数与方法参数一致
*/
String translateText = (String) call.invoke(
new Object[] { "hello" });
System.
out.println(translateText);
}
catch (ServiceException e) {
e.printStackTrace();
System.
out.println("Service 获取 Call对象失败!");
}
catch (MalformedURLException e) {
e.printStackTrace();
System.
out.println("new java.net.URL(url)错误!");
}
catch (RemoteException e) {
e.printStackTrace();
System.
out.println("远程错误!");
}
}
}
|