1. Generate client code
Go to the src directory of the project under cmd: cd d: \ workspaces \ myeclipse 10 \ webservice_cxf_client \ srcd: generate the client code wsdl2java wdsl address
wsdl2java http://localhost:8080/WebService_CXF/WebService/userManager/UserServiceWs?wsdl
2. Write a client Interceptor (add a user name to tell the server that it is a valid caller)
Package interceptor; import Java. util. list; import javax. XML. namespace. QNAME; import Org. apache. cxf. binding. soap. soapmessage; import Org. apache. cxf. headers. header; import Org. apache. cxf. helpers. domutils; import Org. apache. cxf. interceptor. fault; import Org. apache. cxf. phase. abstractphaseinterceptor; import Org. apache. cxf. phase. phase; import Org. apache. log4j. logger; import Org. w3C. dom. document; import Org. w3C. dom. element; public class authinterceptor extends actphaseinterceptor <soapmessage> {logger = logger. getlogger (authinterceptor. class); Private string username; private string password; Public authinterceptor (string username, string password) {// call Super (phase. prepare_send); this. username = username; this. password = PASSWORD ;}@ overridepublic void handlemessage (soapmessage MSG) throws fault {list
3. Spring Configuration
<? 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-3.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"> <import resource = "classpath: META-INF/cxf. XML "/> <Import resource = "classpath: META-INF/cxf/cxf-extension-soap.xml"/> <import resource = "classspath: META-INF/cxf/cxf-servlet.xml"/> <! -- Bean ID, published address, implemented interface, interceptor 1, interceptor 2 --> <jaxws: Client ID = "userwsclient" address = "http: // localhost: 8080/webservice_cxf/WebService/usermanager/userservicews "serviceclass =" com. cxf. WS. iuserservicews "> <jaxws: outinterceptors> <Bean class =" interceptor. authinterceptor "> <constructor-Arg value =" Rain "/> <constructor-Arg value =" Rain "/> </bean> <Bean class =" org. apache. cxf. interceptor. loggingoutinterceptor "/> </jaxws: outinterceptors> </jaxws: client> </beans>
4 Test
Package client; import Org. JUnit. beforeclass; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import COM. cxf. WS. iuserservicews; import COM. cxf. WS. userman; public class testclient {@ beforeclasspublic static void setupbeforeclass () throws exception {}@ testpublic void test () {applicationcontext APP = new Cl Asspathxmlapplicationcontext ("spring-test.xml"); iuserservicews service = app. getbean ("userwsclient", iuserservicews. class); userman man = service. getuserbyname ("DDD"); system. out. println (man. getname () + man. getPassword ();} // The format of the sent SOAP message // http: // localhost: 8080/webservice_cxf/WebService/usermanager/userservicews? WSDL // <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/" >/// <soap: header> // <authhead> // <username> rain1 </username> // <password> rain </password> // </authhead> // </soap: header> // <soap: Body> // <NS2. getuserbyname xmlns: NS2. = "http://ws.cxf.com/"> // <username> AA </username> // </NS2: getuserbyname> // </soap: Body> // </soap: envelope>}