1,ClientProxyFactoryBean调用
对于ClientProxyFactoryBean调用,我查找了很多资料,一般采用的方式是:
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(com.qware.useradm.webservice.ShareQueryWebServiceIface.class);
factory.setAddress("http://localhost:8080/mytest/webservice/ceshi");
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
ShareQueryWebServiceIface client = (ShareQueryWebServiceIface) factory.create();
System.out.println("Response: " );
System.out.println("Response: " + client.powerlvldef(xml));
这个方法不能说不正确,但我一直以来都没能调试通过,估计是自己问题,别人也写了关于这个调试的内容,如下:
客户端没有在SPRING里试成功,但写代码也相当简单,Aegis真好:
getBean ("notifyClient");
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(NotifyService.class);
factory.setAddress("http://127.0.0.1:8080/ebnms/NotifyService");
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
NotifyService client = (NotifyService) factory.create();
DoTest (client);
这次,到是CXF的SERVER和CLIENT都可以正常通信了。但我不说也知道啦,PERL又出问题了!
第三步,
又进一步搜,才知道Document, Literal, RPC, Encoding对SOAP消息的影响,这篇文章(中文的)相当好!
大义是RPC/Encoding将方法名称放入了operation节中,并且消息里含有类型信息,不方便检验。
而Document/Literal通过增加WSDL复杂度,将方法名、参数类型全部放入了types一节,方便了处理。
而SOAP::Lite只支持RPC/Encoding的方式,但也有办法让它形成Doc/Lit的消息:点这里。
但,这种方法只支持JAX-WS的服务,Aegis的PERL就会出错了。
所以,不管用哪种要么JAVA的CLIENT和SERVER通信有问题,不然就是把PERL拒之门外。我怀疑是不是CXF的JAX-WS的数组处理有问题,不然Aegis为何不出错?另外,Aegis对PERL的消息不够宽容,本已是Doc/Lit格式,只是带有TYPE信息也会出错。
不知如何解,先记在此,以后回过头来再研究了。
我对上面所说持保留态度,我想应该是我们本身配置的问题,因为jax-ws和Aegis这两种方法本身就存在着区别,但上面的作者确采用相同的配置方式,我觉得肯定不对的,下面是我找到的一篇文章,我感觉这个操作才是正确,但我没进行验证,文章如下:
采用Aegis数据绑定
如果采用simple frontend方式发布复杂的web service 有自定义数据,默认的情况是采用JAXB绑定,如果你不想用注释,那就用Aegis数据绑定。
Aegis是一个快速,基于STAX(Streaming API for XML)的数据绑定,它能使采用代码优先方式发布web service的情况更简单。Aegis 支持接口,集合类(包括Map)和各种数据类型。
1、 编写复杂的业务接口和实现类
public interface HelloWorld {
String sayHello(String text);
String sayUserHello(User user);
List<User> findUsers();
Map<Integer, User> getMapUsers();
}
其实的和普通的java区别,不用写xmlAdapter,也不用注释。
2、 发布web service
只要在发布web service时加上:svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
具体如下:
protected Server() throws Exception
{
HelloWorldImpl helloworldImpl = new HelloWorldImpl();
ServerFactoryBean svrFactory = new ServerFactoryBean();
//设置服务接口类
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/Hello");
//设置服务实现接口类
svrFactory.setServiceBean(helloworldImpl);
//采用Aegis方式进行数据绑定
svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
//创建服务
svrFactory.create();
}
3、 客户端
创建与具体webservice技术无关的业务接口,因为传递的是User接口,所以要在客户端加上
User 接口和UserImple类
public interface User {
String getPassword();
String getUsername();
void setUsername(String username);
void setPassword(String password);
void setUserId(Integer userId);
Integer getUserId();
}
public class UserImpl implements User {
private Integer userId;
private String username;
private String password;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public UserImpl(String username, String password) {
super();
this.username = username;
this.password = password;
}
public UserImpl() {
}
public UserImpl(String username) {
this.username = username;
}
public UserImpl(Integer userId, String username, String password) {
this.userId = userId;
this.username = username;
this.password = password;
}
}
4、 测试,调用web service.
其中最关键要加上:
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
public class Client {
public static void main(String args[]) throws Exception
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
//设置已发布服务接口
factory.setServiceClass(HelloWorld.class);
//为客户端代理bean 设置已经发布的服务地址
factory.setAddress("http://localhost:9000/Hello");
//采用Aegis数据绑定
factory.getServiceFactory().setDataBinding(new AegisDatabinding());
//获取已发布服务接口实例
HelloWorld client = (HelloWorld)factory.create();
System.out.println(client.sayHello("Tom"));
System.out.println(client.sayUserHello(new UserImpl("aaa1111111111","bbb")));
List<User> list = client.findUsers();
Iterator<User> i = list.iterator();
while (i.hasNext()) {
User u = i.next();
System.out.println(u.getUsername());
}
System.out.println("--------------------");
Map<Integer ,User> map = client.getMapUsers();
for (Integer e : map.keySet()) {
// System.out.println(e);
System.out.println(map.get(e).getUsername());
}
System.exit(0);
}
}
5、 与spring集成:
服务器的spring-cxf.xml的配置文件中加上:<simple:dataBinding>部分。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
<simple:server id="helloservice" serviceClass="frontend.HelloWorld" address="/hello">
<simple:serviceBean>
<bean class="frontend.HelloWorldImpl" />
</simple:serviceBean>
<simple:dataBinding>
<bean class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
</simple:dataBinding>
</simple:server>
</beans>
客户端的配置文件修改spring-client.xml如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="frontendClient" class="frontend.HelloWorld"
factory-bean="frontendClientFactory" factory-method="create" />
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" scope="prototype"/>
<bean id="frontendClientFactory" class="org.apache.cxf.frontend.ClientProxyFactoryBean">
<property name ="dataBinding" ref="aegisBean"/>
<property name="serviceClass" value="frontend.HelloWorld" />
<property name="address"
value="http://localhost:9000/Hello" />
</bean>
</beans>
二:JaxWsProxyFactoryBean调用
String xml="";
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(com.qware.useradm.webservice.ShareQueryWebServiceIface.class);
factoryBean.setAddress("http://localhost:8080/mytest/webservice/ceshi");
ShareQueryWebServiceIface client = (ShareQueryWebServiceIface) factoryBean.create();
String response = client.powerlvldef(xml);
我现在采用的就是这种方式,下面是我的xml配置文件:
client文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="mytestWebServiceClient"
serviceClass="com.mytest.webservice.ceshi"
address="http://localhost:8080/mytest/webservice/ceshi" />
</beans>
webservice文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!--
这里是cxf的重点,就是通过这里将接口暴露出去的
address="//HelloWorld"
是接口暴露的地址,按照我的配置,地址应该是 http://localhost:8080/mytest/webservice/HelloWorld?wsdl
这个地址就是将来我们提供给用户访问的接口地址。dbadm这是我的工程名字,webservice是我webservice的路径。
-->
<!-- <jaxws:server id="helloWorld"
serviceClass="com.qware.mytest.webservice.impl.HelloWorldImpl"
address="/HelloWorldWebService">
<jaxws:serviceBean>
<ref bean="comboServiceIface" /> 要暴露的 bean 的引用
</jaxws:serviceBean>
</jaxws:server>
-->
<!--If you want to reference a spring managed-bean, you can write like this:
<bean id="hello" class="demo.spring.impl.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />-->
<bean id="mytestWebService"
class="com.mytest.webservice.impl.ceshiWebServiceImpl">
</bean>
<jaxws:endpoint id="mytestWebServiceEndpoint"
implementor="#mytestWebService" address="/ceshi" />
</beans>
以上两种都是通过url进行调用,下面我主要说下采用类进行调用
三:简单类调用
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"file:WebRoot/WEB-INF/client.xml");
HelloWorldIface client = (HelloWorldIface) appContext.getBean("helloClient");
System.out.println(client.sayHi("jay"));
对于xml的配置可以参照上面的xml配置方式。
四:注入调用
我是将client.xml文件添加到applicationContext.xml文件中,在调用的action中配置属性mytestWebServiceClient,在下面就可以直接通过mytestWebServiceClient对事件进行调用了。
第三、四点我写的很简单,如果有疑问可以说下,我会回复,也许后面我会进行详细的修改,希望和大家共同学习,共同提高。
分享到:
相关推荐
cxf应用demo,代码取例,一共有四个工程,Client,Server,ClientSpring,ServerSpering 且包含所有jar包,直接可以运行
cxf学习资料,基本教程,啊啊啊啊啊啊啊啊啊啊!
本资源为CXF技术的全部jar包,以及cxf整合spring的实例,可拿去学习使用
NULL 博文链接:https://showlike.iteye.com/blog/1005822
包括简单的HelloWorld、复杂对象传递、安全机制、大文件上传(MTOM)、嵌入jetty等。 呵呵,3分不多吧=,=!!
Apache CXF 框架是一个比较有前途的开源 Web Services 框架,也是构建 SOA 架构应用的利器。本书采用案例源码和解说形式全面介绍 Apache CXF 框架的功能。 本书共 15 章,大致分为三个部分。第一部分介绍关于 SOA 和...
CXF SOAP 应用实例
2013版的 <基于Apache CXF构建SOA应用> 源码 Apache CXF是一个开放源码的Web服务框架,提供了一个易于使用,用于开发Web Services标准为基础的编程模型。本书主要介绍Apache CXF在构建SOA架构各个方面的应用说明和...
CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Servlet 容器的支持
关于WebService的实例及相关axis/apache-cxf的应用关于WebService的实例及相关axis/apache-cxf的应用
CXF方式开发和部署webservice应用,CXF方式开发和部署webservice应用,CXF方式开发和部署webservice应用,CXF方式开发和部署webservice应用,
一、Web Services、SOA简介。 二、CXF简介。...5、CXF应用开发。 【a、创建项目骨架。 b、接口类创建。 c、具体类实现。 d、spring配置。 e、web应用配置。 f、应用部署。 g、启动服务。 h、消费服务。】
基于CXF的webservice开发 1、服务器端 Ⅰ)开发web service业务接口,该接口用@WebService修饰; Ⅱ)开发web service业务接口的实现类,也要用@WebService修饰; Ⅲ)使用EndPoint类的静态方法publish()来发布...
gsoap CXF2.7.5 ssh vc++ webservice应用实例的源码。具体内容看博文《gsoap CXF2.7.5 ssh vc++ webservice应用实例(一)(二)(三)(四)》 http://blog.csdn.net/biboheart/article/details/11959935
2. 目前java主流的webService应用以CXF、AXIS2为主; 3. 通过网络渠道的了解,目前CXF的效率要比AXIS2高出至少50%; 4. 另外有一个webService的工具metro的效率比CXF高出10%; 5. CXF的实现资料网上可以随便找出一大...
cxf webservice 应用 希望对大家有用
讲解 cxf如何应用的javaWeb项目中