Cxf learning Summary 2 add interceptor + integrate Spring

Source: Internet
Author: User
1. Custom interceptor
Package COM. cxf. interceptor; import Java. util. list; import javax. XML. soap. soapexception; import Org. apache. cxf. binding. soap. soapmessage; import Org. apache. cxf. headers. header; 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. element; import Org. w3C. dom. nodelist; // abstractphaseint Erceptor can specify the stage at which the interceptor works. Public class userinterceptor extends actphaseinterceptor <soapmessage> {logger = logger. getlogger (userinterceptor. class); // The constructor with parameters that call the parent class will not implicitly call the parent class without parameters. The constructor public userinterceptor () {super (phase. pre_invoke); // intercept the SOAP message before calling.} @ override // The message is the public void handlemessage (soapmessage MSG) of the intercepted message) throws fault {// obtain the list of all header elements 

 

2. Web. xml configuration file
<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "3.0" 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_3_0.xsd"> <display-Name> </display-Name> <! -- Spring configuration file --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value>/WEB-INF/config/Context /*. XML </param-value> </context-param> <! -- Spring listener --> <listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class> </listener> <! -- Cxfservlet access address --> <servlet-Name> cxfservlet </servlet-Name> <servlet-class> Org. apache. cxf. transport. servlet. cxfservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> cxfservlet </servlet-Name> <URL-pattern>/WebService/* </URL -Pattern> </servlet-mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>

 

3. Spring configuration file

Xmlns: jaxws = "http://cxf.apache.org/jaxws" to see the tag defined under cxf

 

<? 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"/> <! -- Service --> <bean id = "userserviceimpl" class = "com. cxf. Service. impl. userserviceimp"> </bean> <! -- WebService --> <bean id = "userservicews" class = "com. cxf. WS. impl. userservicews "> <property name =" userservice "ref =" userserviceimpl "> </property> </bean> <! -- Interceptor --> <bean id = "userinterceptor" class = "com. cxf. Interceptor. userinterceptor"/> <! -- Publish address, implemented interface, implementation class, interceptor --> <jaxws: Server id = "usermanager" address = "/usermanager" serviceclass = "com. cxf. WS. iuserservicews "> <jaxws: servicebean> <ref bean =" userservicews "/> </jaxws: servicebean> <jaxws: ininterceptors> <ref bean = "userinterceptor"/> </jaxws: ininterceptors> </jaxws: Server> </beans>

 

Access address

Http: // localhost: 8080/webservice_cxf/WebService/usermanager/userservicews? WSDL

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.