xfire客户端对返回list很挑剔,所以需要使用泛型。
2009-03-21 06:37
295 查看
cas+ldap异常分析 | xfire+webservice(服务器配置篇)
2008-12-31
如何建立webservice client
步骤1,建立webservice project
下一步
下一步,默认
选择xfire包
finish, 这样,webservice项目就完成了
接下来是如何进行客户端开发.
在项目中右键,new webservice client
接着,选择你服务文件,wsdl
完成。
这样在你的项目中,会根据wsdl服务文件,生成客户端所需要的内容。
自动生成文件,下去就细细体会,给出一个测试代码
Java代码
package com.seavision.huayi2.service;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
import com.seavision.huayi2.domain.ArrayOfTBusinsessLog;
import com.seavision.huayi2.domain.TBusinsessLog;
import com.seavision.huayi2.domain.TStationMonthReport;
public class IWebserviceClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;
public IWebserviceClient() {
create0();
Endpoint IWebservicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "xfire.local://IWebservice");
endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), IWebservicePortTypeLocalEndpointEP);
Endpoint IWebserviceHttpPortEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://localhost:8080/seavision/services/IWebservice");
endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), IWebserviceHttpPortEP);
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}
public Collection getEndpoints() {
return endpoints.values();
}
private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface", true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((com.seavision.huayi2.service.IWebservicePortType.class), props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "urn:xfire:transport:local");
}
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
}
}
public IWebservicePortType getIWebservicePortTypeLocalEndpoint() {
return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint")));
}
public IWebservicePortType getIWebservicePortTypeLocalEndpoint(String url) {
IWebservicePortType var = getIWebservicePortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public IWebservicePortType getIWebserviceHttpPort() {
return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort")));
}
public IWebservicePortType getIWebserviceHttpPort(String url) {
IWebservicePortType var = getIWebserviceHttpPort();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public static void main(String[] args) {
IWebserviceClient client = new IWebserviceClient();
//create a default service endpoint
IWebservicePortType service = client.getIWebserviceHttpPort();
/**
* 使用ObjectFactory插入
* */
com.seavision.huayi2.domain.ObjectFactory cfactory=new com.seavision.huayi2.domain.ObjectFactory();
com.seavision.huayi2.domain.TStationMonthReport newTStationMonthReport = cfactory.createTStationMonthReport();
newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864"));
//
/**
* 使用JAXBElement插入
* */
// JAXBElement<String> id = new JAXBElement<String>(new
// QName("http://domain.huayi2.seavision.com", "stationMonthReportId"),String.class,"gefa");
// newTStationMonthReport.setStationMonthReportId(id);
service.insertYueJiHua(newTStationMonthReport);
/**
* 测试返回list结果
* */
ArrayOfTBusinsessLog aot = service.getYeWuList();
List list = aot.getTBusinsessLog();
System.out.println(list.size());
for(int i=0;i<list.size();i++){
TBusinsessLog tlog = (TBusinsessLog) list.get(i);
System.out.println(i+"++++++"+tlog.getLogContent().getValue());
}
//TODO: Add custom client code here
//
//service.yourServiceOperationHere();
System.out.println("test client completed");
System.exit(0);
}
}
注意以下内容,1:返回list内容,先存放入Arrayof**对象当中,再进行list遍历。
2: 对象属性取值,getValue();
3:对象属性封装,通过JAXBElement进行封装后,放入对象。
16:25
浏览 (168)
论坛浏览 (373)
评论 (1)
收藏
相关推荐
2008-12-31
xfire+webservice(客户端调用篇)
服务接口,就是用来调用的,所以客户端显得尤为重要,xfire客户端对返回list很挑剔,所以需要使用泛型。如何建立webservice client
步骤1,建立webservice project
下一步
下一步,默认
选择xfire包
finish, 这样,webservice项目就完成了
接下来是如何进行客户端开发.
在项目中右键,new webservice client
接着,选择你服务文件,wsdl
完成。
这样在你的项目中,会根据wsdl服务文件,生成客户端所需要的内容。
自动生成文件,下去就细细体会,给出一个测试代码
Java代码
package com.seavision.huayi2.service;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
import com.seavision.huayi2.domain.ArrayOfTBusinsessLog;
import com.seavision.huayi2.domain.TBusinsessLog;
import com.seavision.huayi2.domain.TStationMonthReport;
public class IWebserviceClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;
public IWebserviceClient() {
create0();
Endpoint IWebservicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "xfire.local://IWebservice");
endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), IWebservicePortTypeLocalEndpointEP);
Endpoint IWebserviceHttpPortEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://localhost:8080/seavision/services/IWebservice");
endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), IWebserviceHttpPortEP);
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}
public Collection getEndpoints() {
return endpoints.values();
}
private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface", true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((com.seavision.huayi2.service.IWebservicePortType.class), props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "urn:xfire:transport:local");
}
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
}
}
public IWebservicePortType getIWebservicePortTypeLocalEndpoint() {
return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint")));
}
public IWebservicePortType getIWebservicePortTypeLocalEndpoint(String url) {
IWebservicePortType var = getIWebservicePortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public IWebservicePortType getIWebserviceHttpPort() {
return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort")));
}
public IWebservicePortType getIWebserviceHttpPort(String url) {
IWebservicePortType var = getIWebserviceHttpPort();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public static void main(String[] args) {
IWebserviceClient client = new IWebserviceClient();
//create a default service endpoint
IWebservicePortType service = client.getIWebserviceHttpPort();
/**
* 使用ObjectFactory插入
* */
com.seavision.huayi2.domain.ObjectFactory cfactory=new com.seavision.huayi2.domain.ObjectFactory();
com.seavision.huayi2.domain.TStationMonthReport newTStationMonthReport = cfactory.createTStationMonthReport();
newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864"));
//
/**
* 使用JAXBElement插入
* */
// JAXBElement<String> id = new JAXBElement<String>(new
// QName("http://domain.huayi2.seavision.com", "stationMonthReportId"),String.class,"gefa");
// newTStationMonthReport.setStationMonthReportId(id);
service.insertYueJiHua(newTStationMonthReport);
/**
* 测试返回list结果
* */
ArrayOfTBusinsessLog aot = service.getYeWuList();
List list = aot.getTBusinsessLog();
System.out.println(list.size());
for(int i=0;i<list.size();i++){
TBusinsessLog tlog = (TBusinsessLog) list.get(i);
System.out.println(i+"++++++"+tlog.getLogContent().getValue());
}
//TODO: Add custom client code here
//
//service.yourServiceOperationHere();
System.out.println("test client completed");
System.exit(0);
}
}
package com.seavision.huayi2.service; import java.net.MalformedURLException; import java.util.Collection; import java.util.HashMap; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import org.codehaus.xfire.XFireRuntimeException; import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.jaxb2.JaxbTypeRegistry; import org.codehaus.xfire.service.Endpoint; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.soap.AbstractSoapBinding; import org.codehaus.xfire.transport.TransportManager; import com.seavision.huayi2.domain.ArrayOfTBusinsessLog; import com.seavision.huayi2.domain.TBusinsessLog; import com.seavision.huayi2.domain.TStationMonthReport; public class IWebserviceClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory(); private HashMap endpoints = new HashMap(); private Service service0; public IWebserviceClient() { create0(); Endpoint IWebservicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "xfire.local://IWebservice"); endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), IWebservicePortTypeLocalEndpointEP); Endpoint IWebserviceHttpPortEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://localhost:8080/seavision/services/IWebservice"); endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), IWebserviceHttpPortEP); } public Object getEndpoint(Endpoint endpoint) { try { return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl()); } catch (MalformedURLException e) { throw new XFireRuntimeException("Invalid URL", e); } } public Object getEndpoint(QName name) { Endpoint endpoint = ((Endpoint) endpoints.get((name))); if ((endpoint) == null) { throw new IllegalStateException("No such endpoint!"); } return getEndpoint((endpoint)); } public Collection getEndpoints() { return endpoints.values(); } private void create0() { TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager()); HashMap props = new HashMap(); props.put("annotations.allow.interface", true); AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry())); asf.setBindingCreationEnabled(false); service0 = asf.create((com.seavision.huayi2.service.IWebservicePortType.class), props); { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "urn:xfire:transport:local"); } { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://schemas.xmlsoap.org/soap/http"); } } public IWebservicePortType getIWebservicePortTypeLocalEndpoint() { return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"))); } public IWebservicePortType getIWebservicePortTypeLocalEndpoint(String url) { IWebservicePortType var = getIWebservicePortTypeLocalEndpoint(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public IWebservicePortType getIWebserviceHttpPort() { return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"))); } public IWebservicePortType getIWebserviceHttpPort(String url) { IWebservicePortType var = getIWebserviceHttpPort(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public static void main(String[] args) { IWebserviceClient client = new IWebserviceClient(); //create a default service endpoint IWebservicePortType service = client.getIWebserviceHttpPort(); /** * 使用ObjectFactory插入 * */ com.seavision.huayi2.domain.ObjectFactory cfactory=new com.seavision.huayi2.domain.ObjectFactory(); com.seavision.huayi2.domain.TStationMonthReport newTStationMonthReport = cfactory.createTStationMonthReport(); newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864")); // /** * 使用JAXBElement插入 * */ // JAXBElement<String> id = new JAXBElement<String>(new // QName("http://domain.huayi2.seavision.com", "stationMonthReportId"),String.class,"gefa"); // newTStationMonthReport.setStationMonthReportId(id); service.insertYueJiHua(newTStationMonthReport); /** * 测试返回list结果 * */ ArrayOfTBusinsessLog aot = service.getYeWuList(); List list = aot.getTBusinsessLog(); System.out.println(list.size()); for(int i=0;i<list.size();i++){ TBusinsessLog tlog = (TBusinsessLog) list.get(i); System.out.println(i+"++++++"+tlog.getLogContent().getValue()); } //TODO: Add custom client code here // //service.yourServiceOperationHere(); System.out.println("test client completed"); System.exit(0); } }
注意以下内容,1:返回list内容,先存放入Arrayof**对象当中,再进行list遍历。
2: 对象属性取值,getValue();
3:对象属性封装,通过JAXBElement进行封装后,放入对象。
16:25
浏览 (168)
论坛浏览 (373)
评论 (1)
收藏
相关推荐
相关文章推荐
- 使用Map把sql返回的list<Object>转成需要的实体对象
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- 使用泛型解决需要动态返回类型的问题
- 如何使用xFire开发webService返回复杂对象List
- 关于在Webservice里使用LinqToSQL遇到一对多关系的父子表中子表需要ToList输出泛型而产生循环引用错误的解决办法!(转)
- XFire客户端解析服务端返回List
- 如何使用xFire开发webService返回复杂对象List
- mac自带了svn服务端和客户端,所以只需要简单配置一下就可以使用
- 使用泛型解决需要动态返回类型的问题
- C#泛型-使用泛型List
- 使用PHP处理数据库数据如何将数据返回客户端并显示当前状态
- 使用泛型后的List集合类型的三种遍历方式
- java中的方法返回值使用泛型,实现灵活的返回值类型
- 使用 ResultSet 遍历查询结果(返回list结果集,重新toString()方法)
- 使用XFire开发Web Service客户端完整入门教程
- 使用spring mvc前台获取后台返回的List
- 客户端在浏览网站时收到“HTTP 403.4 - 禁止访问:需要使用 SSL 查看该资源”错误,但是网站没有配置为使用 SSL
- 使用Solution添加list模板并根据需要自动创建列表
- Asp.Net 中使用客户端Activex控件需要注意的事情
- 如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好