Cxf support for Interceptor interceptor

Source: Internet
Author: User

Do something before or after each request response. The interceptor here is similar to the interceptor of filter and struts. The main function of interceptor is to reduceCodeCoupling to provide code cohesion. Let's take a look at how the cxf interceptor works.

1. We will use the following helloworldservice and re-write the call code of the client. The Code is as follows:

Package COM. hoo. client; import Org. apache. cxf. interceptor. loggingininterceptor; import Org. apache. cxf. interceptor. loggingoutinterceptor; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import COM. hoo. service. ihelloworldservice; public class servicemessageinterceperclient {public static void main (string ARGs []) {// call WebService jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean (); factory. setserviceclass (ihelloworldservice. class); factory. setaddress ("http: // localhost: 9000/helloworld"); factory. getininterceptors (). add (New loggingininterceptor (); factory. getoutinterceptors (). add (New loggingoutinterceptor (); ihelloworldservice service = (ihelloworldservice) factory. create (); system. out. println ("[Result]" + service. sayhello ("e4200003458 "));}}

The interceptor of the preceding cxf is added to the client, and interceptor can also be added to the server. The result is as follows:

23:40:08 org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromclass
Information: creating service {http://service.hoo.com/~ihelloworldserviceservice from class com. Hoo. Service. ihelloworldservice
23:40:09 org. Apache. cxf. Interceptor. abstractlogginginterceptor log
Information: Outbound message
---------------------------
ID: 1
Address: http: // localhost: 9000/helloworld
Encoding: UTF-8
Content-Type: text/XML
Headers: {soapaction = [""], accept = [*/*]}
Payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Body> <NS1: sayhello xmlns: NS1 = "http://service.hoo.com/"> <Name> e4200003458 </Name> </NS1: sayhello> </soap: Body> </soap: envelope>
--------------------------------------
23:40:09 org. Apache. cxf. Interceptor. abstractlogginginterceptor log
Information: Inbound message
----------------------------
ID: 1
Response-code: 200
Encoding: UTF-8
Content-Type: text/XML; charset = UTF-8
Headers: {Content-Type = [text/XML; charset = UTF-8], Content-Length = [234], Server = [Jetty (7.2.2.v20101205)]}
Payload: <soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope/"> <soap: Body> <NS1: sayhelloresponse xmlns: NS1 = "http://service.hoo.com/"> <return> e424173458say: hello World </return> </NS1: sayhelloresponse> </soap: Body> </soap: envelope>
--------------------------------------
[Result] e42100003458say: Hello World

The above information is the log information output by logginginterceptor. The log information is output in the request and response respectively, as well as the parameter information in the output request and the returned value information in the response.

2. I just added an interceptor to the client. Now we write an interceptor. This interceptor must inherit abstractphaseinterceptor to implement handlemessage and a constructor with parameters. Then add the Interceptor to the server.
The interceptor code is as follows:

Package COM. hoo. interceptor; import Org. apache. cxf. interceptor. fault; import Org. apache. cxf. message. message; import Org. apache. cxf. phase. abstractphaseinterceptor;/*** custom message interceptor * @ author administrator * @ createdate 5:28:38 * @ file messageinterceptor. java * @ package COM. hoo. interceptor * @ project cxf_demo02 */public class messageinterceptor extends actphaseinterceptor <message >{// at least one Public messageinterceptor (string phase) {super (phase);} public void handlemessage (message) throws fault {system. out. println ("########### handlemessage ##########"); system. out. println (Message); If (message. getdestination ()! = NULL) {system. Out. println (message. GETID () + "#" + message. getdestination (). getmessageobserver ();} If (message. getexchange ()! = NULL) {system. out. println (message. getexchange (). getinmessage () + "#" + message. getexchange (). getinfaultmessage (); system. out. println (message. getexchange (). getoutmessage () + "#" + message. getexchange (). getoutfaultmessage ());}}}

let's take a look at the Code for publishing a service and adding a custom Interceptor:

Package COM. hoo. delpoy; import Org. apache. cxf. jaxws. jaxwsserverfactorybean; import Org. apache. cxf. phase. phase; import COM. hoo. interceptor. messageinterceptor; import COM. hoo. service. helloworldservice; public class deployinterceptorservice {public static void main (string ARGs []) throws interruptedexception {// publish WebService jaxwsserverfactorybean factory = new jaxwsserverfactorybean (); // set service class factory. setserviceclass (helloworldservice. class); factory. setaddress ("http: // localhost: 9000/helloworld"); // set servicebean object factory. setservicebean (New helloworldservice (); // Add the request and the corresponding interceptor, phase. receive is only valid for in, phase. send is only valid for out factory. getininterceptors (). add (New messageinterceptor (phase. receive); factory. getoutinterceptors (). add (New messageinterceptor (phase. send); factory. create (); system. out. println ("Server start ...... "); thread. sleep (1000*60); system. exit (0); system. out. println ("server exit ");}}

It is worth noting that in the past, WebService was released using the endpoint push method. Jaxwsserverfactorybean is used here and the code called by the client is a little different from jaxwsproxyfactorybean.

Client call code:

 
Package COM. hoo. client; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import COM. hoo. service. ihelloworldservice; public class helloworldserviceclient {public static void main (string [] ARGs) {// call WebService jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean (); factory. setserviceclass (ihelloworldservice. class); factory. setaddress ("http: // localhost: 9000/helloworld"); ihelloworldservice service = (ihelloworldservice) factory. create (); system. out. println ("[Result]" + service. sayhello ("hoojo "));}}

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.