One: Depends on the jar package: apache-cxf-2.7.10, can be downloaded from the official website
Two:. Writing a Web Services server: requires an interface called Hello, the code is as follows: Add @webservice comment before class name
Package com; Import Javax.jws.WebService; @WebService Public Interface Hello { public string SayHello (String str); }
Three: Write an interface implementation class: include two parameters of the pass, one is the concatenation of the XML string
Package Com;import Java.util.hashmap;import Java.util.Map;
@WebService (endpointinterface = "com. Hello ") public class Helloimpl implements Hello { @Override public string SayHello (String str) { SYSTEM.OUT.PRINTLN ("Web services Invocation succeeded"); String result = Createxml (str); return result; } /** * generates the corresponding XML based on the string passed. * * @param passed the parameter name * @return returns the XML string * /public string Createxml (string name) { StringBuffer sb = new StringBuffer (); Sb.append ("<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>"); Sb.append ("<Result>"); Sb.append ("<cinamaName> hehe </cinamaName>"); Sb.append ("<director>" + name + "</director>"); Sb.append ("<introduce> I am the description </introduce>"); Sb.append ("<price>25</price>"); Sb.append ("</Result>"); return sb.tostring (); }
Four: Write the startup class for Web services, the code below, run, Web services to query:
Public Static void Main (string[] args) { try { = dynamicclientfactory.newinstance (); = Dcf.createclient ("http://localhost:18080/aiplat/service/helloservice?wsdl"); object[] Objs = Client.invoke ("SayHello", "test");
Receive results
System.out.println (objs[0
Catch (Exception e) {
E.printstacktrace ();
}
}
Spring Configuration
<jaxws:server id= "helloservice" serviceclass= "com. Hello "address="/helloservice "> <jaxws:serviceBean> <ref bean=" Helloimpl "/> </ Jaxws:servicebean> </jaxws:server>
Parsing of XML
/*** Parse XML string * *@paramthe XML string passed by Str.*/ Public Static voidparserxml (String str) {Try{StringReader Read=NewStringReader (str); //Creating a new input source The SAX parser uses the InputSource object to determine how to read the XML inputInputSource Source =NewInputSource (read); //Create a new SaxbuilderDocumentbuilderfactory DBF =documentbuilderfactory.newinstance (); Documentbuilder DB=Dbf.newdocumentbuilder (); Document Document=db.parse (source); NodeList Movies=document.getchildnodes (); for(inti = 0; I < movies.getlength (); i++) {Node employee=Movies.item (i); NodeList EmployeeInfo=employee.getchildnodes (); for(intj = 0; J < Employeeinfo.getlength (); J + +) {node node=Employeeinfo.item (j); if(Node.getnodetype () = =Node.element_node) { if("Cinamaname". Equals (Node.getnodename ())) {System.out.println (Node.gettextcontent ()); } Else if("Introduce". Equals (Node.getnodename ())) {System.out.println (Node.gettextcontent ()); } Else if("Price". Equals (Node.getnodename ())) {System.out.println (Node.gettextcontent ()); } Else if("Director". Equals (Node.getnodename ())) {System.out.println (Node.gettextcontent ()); } } } } } Catch(FileNotFoundException e) {System.out.println (E.getmessage ()); } Catch(parserconfigurationexception e) {System.out.println (E.getmessage ()); } Catch(saxexception e) {System.out.println (E.getmessage ()); } Catch(IOException e) {System.out.println (E.getmessage ()); } }
CXF Framework for Web Service learning, passing XML and parsing