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真好:
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对事件进行调用了。
第三、四点我写的很简单,如果有疑问可以说下,我会回复,也许后面我会进行详细的修改,希望和大家共同学习,共同提高。