The server is a web project and the client is a Java project. Check the server code first.
The first is sei, that is, helloservice. Java, the server interface class.
package com.jadyer.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebService(targetNamespace="http://blog.csdn.net/jadyer")public interface HelloService {@WebMethod@WebResult(name="sayHelloResult")public String sayHello(@WebParam(name="name")String name);}
Then there is SIB, that is, the server interface implementation class helloserviceimpl. Java
package com.jadyer.service;import javax.jws.WebService;import org.springframework.stereotype.Service;@WebService(endpointInterface="com.jadyer.service.HelloService", targetNamespace="http://blog.csdn.net/jadyer")@Servicepublic class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {System.out.println("Receive the name=[" + name + "]");if(null==name){return "Hello,World";}else{return "Hello," + name;}}}
The following is the server-defined interceptor class licenseininterceptor. java.
package com.jadyer.interceptor;import java.util.List;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;import org.apache.cxf.headers.Header;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.phase.Phase;import org.springframework.stereotype.Component;import org.w3c.dom.Node;@Componentpublic class LicenseInInterceptor extends AbstractSoapInterceptor {public LicenseInInterceptor(){super(Phase.INVOKE);}@Overridepublic void handleMessage(SoapMessage message) throws Fault {List<Header> headers = message.getHeaders();Object obj = null;for(Header header : headers){if(header.getName().getLocalPart().equals("licenseInfo")){obj = header.getObject();if(obj instanceof Node){System.out.println("Receive the licenseInfo=[" + ((Node)obj).getTextContent() + "]");}}}}}
The following is the spring configuration file applicationcontext. xml of the server.
<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: jaxws = "http://cxf.apache.org/jaxws" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.sprin Gframework.org/schema/context/spring-context-2.5.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <context: component-scan base-package =" com. jadyer "/> <import resource =" classpath: META-INF/cxf. XML "/> <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> <! -- Publish the WebServices service --> <! -- You can also write <jaxws: endpoint implementor = "com. jadyer. Service. helloserviceimpl" address = "/myhello"/> --> <! -- The last access address is http: // 127.0.0.1: 8088/webpath/services/myhello? WSDL --> <! -- After testing, it is also possible to write address = "myhello". The access address is the same as the one posted on the previous line --> <jaxws: endpoint implementor = "# helloserviceimpl" address = "/myhello"> <jaxws: ininterceptors> <Bean class = "org. apache. cxf. interceptor. loggingininterceptor "/> <ref bean =" licenseininterceptor "/> </jaxws: ininterceptors> <jaxws: outinterceptors> <Bean class =" org. apache. cxf. interceptor. loggingoutinterceptor "/> </jaxws: outinterceptors> </jaxws: endpoint> <! -- You can also use the core. XSD provided by cxf to register an interceptor. For an example, see the client code --> <! -- Xmlns: cxf = "http://cxf.apache.org/core" --> <! -- Xsi: schemalocation = "http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd" --> <! -- Write it as follows: <jaxws: endpoint implementor = "# helloserviceimpl" address = "/myhello"/> <cxf: Bus> <cxf: ininterceptors> <Bean class = "org. apache. cxf. interceptor. loggingininterceptor "/> <ref bean =" licenseininterceptor "/> </cxf: ininterceptors> <cxf: outinterceptors> <Bean class =" org. apache. cxf. interceptor. loggingoutinterceptor "/> </cxf: outinterceptors> </cxf: Bus> --> </beans>
This is log4j. properties used by the server to conveniently view spring logs.
# Running hh: mm: SS}] [% T] [% c {1}] % m % N
Finally, the Web. xml of the server
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping></web-app>
OK. The server code example is complete. The following is the client code.
First, the interceptor class licenseoutinterceptor. java used by the client to send soapheader Information
package com.jadyer.interceptor;import javax.xml.bind.JAXBException;import javax.xml.namespace.QName;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;import org.apache.cxf.databinding.DataBinding;import org.apache.cxf.headers.Header;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.jaxb.JAXBDataBinding;import org.apache.cxf.phase.Phase;import org.springframework.stereotype.Component;@Componentpublic class LicenseOutInterceptor extends AbstractSoapInterceptor{public LicenseOutInterceptor(){super(Phase.WRITE);}@Overridepublic void handleMessage(SoapMessage message) throws Fault {QName qname = new QName("http://blog.csdn.net/jadyer", "licenseInfo", "ns");DataBinding dataBinding = null;try {dataBinding = new JAXBDataBinding(String.class);} catch (JAXBException e) {e.printStackTrace();}Header header = new Header(qname, "Jadyer", dataBinding);message.getHeaders().add(header);}}
The following is a service implementation class clientservice. Java simulated by the client.
package com.jadyer.service;import javax.annotation.Resource;import net.csdn.blog.jadyer.HelloService;import org.springframework.stereotype.Service;@Servicepublic class ClientService {@Resourceprivate HelloService myServerWebService;public String getServerResponse(String name){return myServerWebService.sayHello(name);}}
The following is the spring configuration file applicationcontext. xml of the client.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:cxf="http://cxf.apache.org/core"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><context:component-scan base-package="com.jadyer"/><import resource="classpath:META-INF/cxf/cxf.xml"/><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/><import resource="classpath:META-INF/cxf/cxf-servlet.xml"/><jaxws:client id="myServerWebService" serviceClass="net.csdn.blog.jadyer.HelloService"address="http://127.0.0.1:8088/cxf_04_spring/services/myHello"/><cxf:bus><cxf:inInterceptors><bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/></cxf:inInterceptors><cxf:outInterceptors><bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><ref bean="licenseOutInterceptor"/></cxf:outInterceptors></cxf:bus></beans>
Finally, the client calls the simulation entry clientapp. Java of the server.
Note: The specific client WebService code is generated by wsdl2java.
Package COM. jadyer. client; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import COM. jadyer. service. clientservice; public class clientapp {public static void main (string [] ARGs) {// wsdl2java-D:/download/-frontend jaxws21-keep-verbose http: // 127.0.0.1: 8088/cxf_04_spring/services/myhello? WSDL // wsdl2java command is similar to wsimport, wsimport Introduction For details, see http://blog.csdn.net/jadyer/article/details/8692108ApplicationContext CTX = new classpathxmlapplicationcontext ("applicationcontext. XML "); clientservice cs = (clientservice) CTX. getbean ("clientservice"); system. out. println (CS. getserverresponse (" "));}}
Old habit: post the console output as well
This is the console output of the server.
23:26:08 Org. apache. cxf. services. helloserviceimplservice. helloserviceimplport. helloservice information: Inbound message -------------------------- ID: 1 address: http: // 127.0.0.1: 8088/cxf_04_spring/services/myhelloencoding: UTF-8Http-Method: postcontent-type: text/XML; charset = UTF-8Headers: {accept = [*/*], cache-control = [no-Cache], connection = [keep-alive], Content-Length = [302], content-Type = [text/XML; charset = UTF-8], host = [127.0.0.1: 8088], Pragma = [no-Cache], soapaction = [""], user-Agent = [Apache cxf 2.7.0]} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Header> <ns: licenseinfo xmlns: NS = "http://blog.csdn.net/jadyer"> jadyer </ns: licenseinfo> </soap: Header> <soap: Body> <NS2: sayhello xmlns: <Name> Xuan Yu </Name> </NS2: sayhello> </soap: Body> </soap: envelope> ------------------------------------ receive the name = [Xuan Yu] receive the licenseinfo = [jadyer] 23:26:08 Org. apache. cxf. services. helloserviceimplservice. helloserviceimplport. helloservice information: Outbound message ------------------------- ID: 1 encoding: UTF-8Content-Type: text/xmlheaders :{} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: body> <ns2-sayhelloresponse xmlns: ns2-= "http://blog.csdn.net/jadyer"> <sayhelloresult> hello, Xuan Yu </sayhelloresult> </ns2-sayhelloresponse> </soap: body> </soap: envelope> --------------------------------------
This is the console output of the client.
23:26:08 Org. apache. cxf. services. helloserviceservice. helloserviceport. helloservice information: Outbound message ------------------------- ID: 1 address: http: // 127.0.0.1: 8088/cxf_04_spring/services/myhelloencoding: UTF-8Http-Method: postcontent-type: text/xmlheaders: {accept = [*/*], soapaction = [""]} payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Header> <ns: licenseinfo xmlns: NS = "http://blog.csdn.net/jadyer"> jadyer </ns: licenseinfo> </soap: Header> <soap: Body> <ns2-sayhello xmlns: <Name> Xuan Yu </Name> </NS2: sayhello> </soap: Body> </soap: envelope> -------------------------------------------- 23:26:08 Org. apache. cxf. services. helloserviceservice. helloserviceport. helloservice information: Inbound message -------------------------- ID: 1response-code: 200 encoding: UTF-8Content-Type: text/XML; charset = UTF-8Headers: {Content-Type = [text/XML; charset = UTF-8], date = [Fri, 31 May 2013 15:26:08 GMT], Server = [Apache-Coyote/1.1], transfer-encoding = [chunked]} payload: <soap: envelope xmlns: soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Body> <ns2-: sayhelloresponse xmlns: ns2-= "http://blog.csdn.net/jadyer"> <sayhelloresult> hello, Xuan Yu </sayhelloresult> </NS2: sayhelloresponse> </soap: Body> </soap: envelope> -------------------------------------- hello, Xuan Yu