CXF Creating dynamic WebService

Source: Internet
Author: User

D:\developTools\apache-cxf-2.5.2\samples\wsdl_first_dynamic_clientCXF method Cxf Method Serviceinfo.getbindings () Bindinginfo--[bindinginfo http://schemas.xmlsoap.org/wsdl/soap/]serviceinfo.getdocumentation () WS_0917_05package Com.test;import Java.beans.propertydescriptor;import Java.io.file;import Java.lang.reflect.method;import Java.net.url;import Java.util.arraylist;import Java.util.collection;import Java.util.hashmap;import Java.util.iterator;import Java.util.list;import Java.util.map;import Java.util.set;import Javax.xml.bind.jaxbelement;import Javax.xml.namespace.qname;import Org.apache.cxf.endpoint.client;import Org.apache.cxf.endpoint.clientimpl;import Org.apache.cxf.endpoint.endpoint;import org.apache.cxf.jaxb.JAXBUtils; Import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;import Org.apache.cxf.service.model.bindinginfo;import Org.apache.cxf.service.model.bindingmessageinfo;import Org.apache.cxf.service.model.bindingoperationinfo;import Org.apache.cxf.service.model.EnDpointinfo;import Org.apache.cxf.service.model.messagepartinfo;import Org.apache.cxf.service.model.ServiceInfo; public class Test {/** * * @throws Exception */public static void main (string[] args) throws Exc      eption {Long begin = System.currenttimemillis ();       String namespace = "";      String service = "Biservicessoap";         String methodName = "drill_ws005"; The URL of the remote webservice String Hosturl = "Http://172.16.66.50:8080/dswsbobje/qaawsservices/biws?wsdl=1&cuid=M1X4xP                    Aaahadapsa.aaamzwaaqwqqjiaaaa ";         Methodname= "Drill_ws_0917_05"; Hosturl = "Http://172.16.66.50:8080/dswsbobje/qaawsservices/biws?"         Wsdl=1&cuid=m1x6lewacbh.apsa.aaan0aaaqwqqjiaaaa ";  try {//System.getproperties (). Put ("ProxyHost", "PROXY.CMCC");//System.getproperties (). Put ("ProxyPort",             "8080");             Create dynamic client Jaxwsdynamicclientfactory factory = Jaxwsdynamicclientfactory.newinstance (); // Create a client connection URL wsdlurl = new URL (hosturl);             Client client = factory.createclient (wsdlurl);             Clientimpl Clientimpl = (clientimpl) client;             Endpoint Endpoint = Clientimpl.getendpoint (); Make use of CXF service model to introspect the existing WSDL serviceinfo serviceinfo = Endpoint.getservice             (). Getserviceinfos (). get (0);             namespace = Serviceinfo.gettargetnamespace ();             Serviceinfo.getname (). Getnamespaceuri ();          Serviceinfo.getproperty ("Operation");          Collection<endpointinfo> ends = Serviceinfo.getendpoints ();          Iterator<endpointinfo> Qeiter=ends.iterator ();           while (Qeiter.hasnext ()) {Endpointinfo einfo= qeiter.next ();          System.out.println (Einfo);            } System.out.println ("" + serviceinfo.getendpoints ());              SYSTEM.OUT.PRINTLN ("Document---" +serviceinfo.getinterface ()); Map<string,object> ProperiEs= serviceinfo.getproperties ();            System.out.println (Properies.size ());             For (String Key:properies.keySet ()) {System.out.println ("key--" +key+ "-----" + properies.get (key). ToString ());}             Set<qname> qset=serviceinfo.getmessages (). KeySet ();             Iterator<qname> Qiter=qset.iterator ();              while (Qiter.hasnext ()) {QName q=qiter.next (); Package name String packagename=jaxbutils.namespaceuritopackage (Serviceinfo.getname (). Getnamespaceuri ()); String Classname=jaxbutils.nametoidentifier (Q.getlocalpart (), JAXBUtils.IdentifierType.INTERFACE);             System.out.println (packagename+ "---className----" +classname);             } String Packagename=jaxbutils.namespaceuritopackage (Serviceinfo.getname (). Getnamespaceuri ()); Object person =thread.currentthread (). Getcontextclassloader (). LoadClass (packagename+ ".                          Lovvalueindex "). newinstance ();           Create QName to specify namespace and service to invoke  QName bindingname = new QName (namespace, service);             Bindinginfo binding = serviceinfo.getbinding (Bindingname);                           Create QName to specify namespace and the method to invoke QName Opname = new QName (namespace, methodName);             Bindingoperationinfo boi = binding.getoperation (opname);             Bindingmessageinfo inputmessageinfo = Boi.getinput ();             list<messagepartinfo> parts = Inputmessageinfo.getmessageparts ();             Gets the object instance messagepartinfo partinfo = parts.get (0);             class<?> Partclass = Partinfo.gettypeclass ();              Object inputobject = Partclass.newinstance ();              Reflectutil.setfieldvalue (inputobject, "login", "Bwadm");             Reflectutil.setfieldvalue (inputobject, "password", "Bwadmbwadm"); Gets the set method of the field and assigns the value list<map<string,string>> listdate = new Arraylist<map<string,string>&gt              ;(); map<string,string> map = new HashmaP<string, string> ();              Map.put ("index", "20160901");              Map.put ("Valueofprompt", "111111");              Listdate.add (map);             Reflectutil.setfieldvalue (Inputobject, "Sales out of stock date", listdate,serviceinfo);             Call the client invoke () method, pass the Inputobject to the method to be called and get the result object object[] result = Client.invoke (Opname, inputobject);             The result obtained is an object headergetter = Reflectutil.getfieldvalue (result[0], "headers");             Object headerrows = Reflectutil.getfieldvalue (Headergetter, "Row");             List<?> headerlist = (list<?>) headerrows;                for (int i = 0; i < headerlist.size (); i++) {Object row = Headerlist.get (i);                Object rowvalues = Reflectutil.getfieldvalue (row, "cell"); System.out.println (rowvalues);            } Object Resultgetter = Reflectutil.getfieldvalue (result[0], "table"); Object rows = Reflectutil.getfieldvalue (Resultgetter, "Row ");            list<?> list = (list<?>) rows;            list<list<object>> RElist = new arraylist<list<object>> ();             for (int i =0; i<list.size (); i++) {Object row = List.get (i); Object rowvalues = Reflectutil.getfieldvalue (row, "cell");//List<object> ROWOBJS = (list<object>             ) rowvalues;//for (int j = 0; J < Rowobjs.size (); j + +) {//System.out.println (Rowobjs.get (j));// } relist.add ((list<object>) rowvalues);                      } System.out.println (RElist);          Long end = System.currenttimemillis ();         System.err.println (End-begin);         } catch (Exception e) {e.printstacktrace (); }}}package com.test;import Java.beans.introspectionexception;import java.beans.propertydescriptor;import Java.lang.reflect.field;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.Method; Import JAVA.lang.reflect.typevariable;import Java.util.hashmap;import Java.util.list;import Java.util.Map;import Javax.xml.bind.jaxbelement;import Javax.xml.namespace.qname;import Org.apache.cxf.jaxb.jaxbutils;import Org.apache.cxf.service.model.serviceinfo;public class Reflectutil {public static map<string,string> Map = new have Hmap<string, string> (); static{map.put ("Login", "String.class"); Map.put ("Password", "String.class"); Map.put ("ResetState", "Boolean.class" ); Map.put ("Refresh", "Boolean.class"); Map.put ("Getfromlatestdocumentinstance", "Boolean.class"); Map.put ("Getfromuserinstance", "Boolean.class"); Map.put ("Turnoutputtovtable", "Boolean.class"); Map.put ("Closedocument", "Boolean.class"); Map.put ("Endrow", "Integer.class"); Map.put ("StartRow", "Integer.class");  public static void SetFieldValue (Object obj, String field,string fieldvalue) {class<?> classs = Obj.getclass (); try {//Takes a field with the Get Set method PropertyDescriptor Partpropertydescriptor = new PropertyDescriptor (fIeld, CLASSS); Method methodsetter = Partpropertydescriptor.getwritemethod (); class<?>[] Paramtypes = Methodsetter.getparametertypes (); String Paramtype = Paramtypes[0].getname (); if (Paramtype.equals ("Javax.xml.bind.JAXBElement")) {if ("String.class". Equals (Map.get (field))) {jaxbelement< string> elestring = new jaxbelement<string> (New QName ("Http://bean.cxf.hs", field), Stri  Ng.class, Fieldvalue); Methodsetter.invoke (obj, elestring); }else if ("Integer.class". Equals (Map.get (field))) {jaxbelement<integer> Eleinteger = new Jaxbelement<integer  > (New QName ("Http://bean.cxf.hs", field), Integer.class, integer.valueof (Fieldvalue)); Methodsetter.invoke (obj, Eleinteger); }else if ("Boolean.class". Equals (Map.get (field))) {jaxbelement<boolean> Eleinteger = new Jaxbelement<boolean  > (New QName ("Http://bean.cxf.hs", field), Boolean.class, boolean.valueof (Fieldvalue)); Methodsetter.invokE (obj, eleinteger);  }}} catch (Exception e) {e.printstacktrace ();}} public static void SetFieldValue (Object obj, String field, list<map<string,string>> data, serviceinfo ServiceInfo) {Object listobj =getfieldvalue (Obj,field);  list<object> list = (list<object>) listobj;  String Packagename=jaxbutils.namespaceuritopackage (Serviceinfo.getname (). Getnamespaceuri ()); try {Class Lovvalueindexclass =thread.currentthread (). Getcontextclassloader (). LoadClass (packagename+ ". Lovvalueindex "); for (int i = 0; i < data.size (); i++) {map<string,string> Map = Data.get (i); Object vi =lovvalueindexclass.newinstance (); SetFieldValue (vi, "Index", Map.get ("index")); SetFieldValue (vi, "Valueofprompt", Map.get ("valueofprompt")); List.add (vi); }} catch (Exception e) {e.printstacktrace ();}} public static object GetFieldValue (Object obj, String field) {object reobj = null; Class<?> classs = Obj.getclass (); try {propertydescriptor resultdescriptor = new PropertydEscriptor (field, classs); Reobj = Resultdescriptor.getreadmethod (). Invoke (obj); } catch (Exception e) {try {method[] methods = Classs.getdeclaredmethods (); for (int i = 0; i < methods.length; i++) {method = Methods[i]; String name = Method.getname (); if (Name.replace ("Get", ""). toLowerCase (). Equals (Field.tolowercase ())) {reobj = Method.invoke (obj);}} } catch (Exception E1) {e1.printstacktrace ();}} return reobj; }}

  

CXF Creating dynamic WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.