?CXF 2.0 学习笔记-1(服务端)? 中生成了服务端,现在把服务端的wsdl保存到本地(直接把Http://xxx/ws/xxx?wsdl?另存为wsdl文件) 。
?? 我们要根据服务端的wsdl来生成客户端调用的文件,这样需要用到CXF BIN目录下wsdl2java.bat 。
???wsdl2java 参数如下:
wsdl2java?-fe?<frontend?name>*?-db?<data?binding?name>*?-wv?<[wsdl?version]>*?
-p?<[wsdl?namespace?=]Package?Name>*?-sn?<service-name>?-b?<binding-name>*?
-catalog?<catalog-file-name>?-d?<output-directory>?-compile?
-classdir?<compile-classes-directory>?-impl?-server?-client?-all?
-autoNameResolution?-defaultValues<=class?name?for?DefaultValueProvider>?-ant?
-nexclude?<schema?namespace?[=?java?packagename]>*??-exsh?<(true,?false)>
-dns?<(true,?false)>?-dex?<(true,?false)>?-validate?-keep?
-wsdlLocation?<wsdlLocation?attribute>?-xjc<xjc?arguments>?-noAddressBinding?-h?
-v?-verbose?-quiet?<wsdlurl>
-p?<[wsdl?namespace?=]Package?Name>*?-sn?<service-name>?-b?<binding-name>*?
-catalog?<catalog-file-name>?-d?<output-directory>?-compile?
-classdir?<compile-classes-directory>?-impl?-server?-client?-all?
-autoNameResolution?-defaultValues<=class?name?for?DefaultValueProvider>?-ant?
-nexclude?<schema?namespace?[=?java?packagename]>*??-exsh?<(true,?false)>
-dns?<(true,?false)>?-dex?<(true,?false)>?-validate?-keep?
-wsdlLocation?<wsdlLocation?attribute>?-xjc<xjc?arguments>?-noAddressBinding?-h?
-v?-verbose?-quiet?<wsdlurl>
? 我们在命令行下输入:wsdl2java -p hs.cxf.client? HelloWorld.wsdl 来指定生成的客户端包目录。
?客户端调用代码
try?{
????????????JaxWsProxyFactoryBean??factory?=?new?JaxWsProxyFactoryBean?();?
????????????factory.setServiceClass(WebServiceSample.class);
????????????factory.setAddress("http://127.0.0.1:9080/cxfTest/ws/HelloWorld");
????????????Object?obj?=?factory.create();
????????????System.out.println(obj?==?null???"NULL":obj.getClass().getName());
????????????if(obj?!=?null){
????????????????WebServiceSample?ws?=?(WebServiceSample)obj;
????????????????String?str?=?ws.say("test");
????????????????System.out.println(str);
????????????????
????????????????str?=?ws.say("1111");
????????????????System.out.println(str);
????????????????
????????????????User?u?=?new?User();
????????????????JAXBElement?<String>?je?=?
????????????????????new?JAXBElement?<String>(
????????????????????????????new?QName("http://bean.cxf.hs","name"),
????????????????????????????String.class,"张三");?
????????????????u.setName(je);
????????????????str?=?ws.sayUserName(u);
????????????????System.out.println(str);
????????????????
????????????????//通过对象来交互
????????????????ReqBean?req?=?new?ReqBean();
????????????????req.setExp(new?JAXBElement?<String>(
????????????????????????????new?QName("http://bean.cxf.hs","exp"),
????????????????????????????String.class,"<exp>111<exp>"));
????????????????req.setSeqId(new?JAXBElement?<String>(
????????????????????????new?QName("http://bean.cxf.hs","seqId"),
????????????????????????String.class,"12345678"));
????????????????RespBean?resp?=?ws.action(req);
????????????????System.out.println("resp_id:"+resp.getRespId().getValue());
????????????????System.out.println("resp_exp:"+resp.getExp().getValue());
????????????}
????????}?catch?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
????????????JaxWsProxyFactoryBean??factory?=?new?JaxWsProxyFactoryBean?();?
????????????factory.setServiceClass(WebServiceSample.class);
????????????factory.setAddress("http://127.0.0.1:9080/cxfTest/ws/HelloWorld");
????????????Object?obj?=?factory.create();
????????????System.out.println(obj?==?null???"NULL":obj.getClass().getName());
????????????if(obj?!=?null){
????????????????WebServiceSample?ws?=?(WebServiceSample)obj;
????????????????String?str?=?ws.say("test");
????????????????System.out.println(str);
????????????????
????????????????str?=?ws.say("1111");
????????????????System.out.println(str);
????????????????
????????????????User?u?=?new?User();
????????????????JAXBElement?<String>?je?=?
????????????????????new?JAXBElement?<String>(
????????????????????????????new?QName("http://bean.cxf.hs","name"),
????????????????????????????String.class,"张三");?
????????????????u.setName(je);
????????????????str?=?ws.sayUserName(u);
????????????????System.out.println(str);
????????????????
????????????????//通过对象来交互
????????????????ReqBean?req?=?new?ReqBean();
????????????????req.setExp(new?JAXBElement?<String>(
????????????????????????????new?QName("http://bean.cxf.hs","exp"),
????????????????????????????String.class,"<exp>111<exp>"));
????????????????req.setSeqId(new?JAXBElement?<String>(
????????????????????????new?QName("http://bean.cxf.hs","seqId"),
????????????????????????String.class,"12345678"));
????????????????RespBean?resp?=?ws.action(req);
????????????????System.out.println("resp_id:"+resp.getRespId().getValue());
????????????????System.out.println("resp_exp:"+resp.getExp().getValue());
????????????}
????????}?catch?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
运行此代码,查看结果即可
依赖包如下:
cxf-2.1.4.jar
jaxb-api-2.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxws-api.jar
stax-api-1.0-2.jar
jaxb-impl-2.1.9.jar
geronimo-activation_1.1_spec-1.0.2.jar
xml-resolver-1.2.jar
XmlSchema-1.4.3.jar
wsdl4j-1.6.2.jar
saaj-api-1.3.jar
jsr173_1.0_ri.jar
jaxb-api-2.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxws-api.jar
stax-api-1.0-2.jar
jaxb-impl-2.1.9.jar
geronimo-activation_1.1_spec-1.0.2.jar
xml-resolver-1.2.jar
XmlSchema-1.4.3.jar
wsdl4j-1.6.2.jar
saaj-api-1.3.jar
jsr173_1.0_ri.jar
其中jsr从bean下载:http://ftpna2.bea.com/pub/downloads/jsr173.jar (注意解压)