Record WebService add interceptors to achieve the purpose of modifying XML messages
import Org.apache.cxf.endpoint.client;import org.apache.cxf.frontend.ClientProxy;PrivateSoap Createsoap () {Service=NewService (); Soap=Service.getservicesoap (); //Interceptor Retrofit Message HeaderClient =clientproxy.getclient (SOAP); Headerininterceptor Soapheaderininterceptor=NewHeaderininterceptor (); Soapheaderininterceptor.sethostuser ("username","Password"); Client.getoutinterceptors (). Add (Soapheaderininterceptor); returnsoap; } Public classHeaderininterceptor extends Abstractsoapinterceptor { PublicHeaderininterceptor () {super (phase.write); } PrivateString msg; @Override Public voidhandlemessage (SoapMessage message) throws Fault {Try{List<Header> headers =message.getheaders (); QName QName=NewQName ("Authorization-my-header");//nodes that need to be increasedDocument document =caretedocument (msg); Element documentelement=document.getdocumentelement (); SoapHeader Head=NewSoapHeader (QName, documentelement); Headers.add (head); } Catch(Exception e) {e.printstacktrace (); } } Public voidsethostuser (String username,string pw) {string str="<tem:authorizationsoapheader soapenv:mustunderstand=\ "1\" xmlns:tem=\ "http://tempuri.org/\ ""+"xmlns:soapenv=\ "http://schemas.xmlsoap.org/soap/envelope/\" >\n"+"\t<tem:username>"+ username+"</tem:username>\n"+"\t<tem:password>"+pw+"</tem:password>\n"+"</tem:AuthorizationSoapHeader>"; This. msg =str; } Public StaticDocument caretedocument (String xml) throws Exception {InputStream strm; STRM=NewBytearrayinputstream (Xml.getbytes ("UTF-8")); Documentbuilderfactory domfactory=documentbuilderfactory. newinstance (); Domfactory.setnamespaceaware (true); Documentbuilder Builder=Domfactory.newdocumentbuilder (); Document Doc=Builder.parse (STRM); returnDoc; }}
PS: In general, use "Wsimport-Clientjar {Xxx.jar}-p {package name}-S {Source package name} {URL}" generated by the client, the creation of the service has no parameter and the method of the argument
No parameter: Generate service for each call to the remote URL
Parameter: Place the WSDL locally and pass the method through the URL to reduce the network overhead per request
Service = new Service (New URL ("file://xxx/xxx.wsdl"));
WebService's Interceptor