JAX-WS Handlerchain using the detailed

Source: Internet
Author: User
Tags date now

JAX-WS Handler, like the servlet filter, can intercept all webservicer, log logs, control permissions, encrypt the requested SOAP message, decrypt, and so on, in handler. JAX-WS provides two handler interfaces, Logicalhandler and Soaphandler. Logicalhandler handles the message Payload and can only access the SOAP message body in the messaging unit. Soaphandler handles the entire SOAP message (which contains the SOAP header and SOAP body) and can access the entire SOAP message.

There are several ways to register handler:

Using Handlerresolver (client is convenient)

Using Handlerchain annotations and configuration files

Generated from WSDL

Declaring Handlerchain with custom binding


WebService execution order in Jax-ws



The following is implemented using Soaphandler to record request content and response content on the WebService server.

Import Java.io.ioexception;import Java.util.set;import Javax.xml.namespace.qname;import Javax.xml.soap.soapexception;import Javax.xml.soap.soapmessage;import Javax.xml.ws.handler.messagecontext;import Javax.xml.ws.handler.soap.soaphandler;import javax.xml.ws.handler.soap.soapmessagecontext;/** * Record SOAP request and response * @ author [email protected] * */public class Loggerhandler implements soaphandler<soapmessagecontext> {@ overridepublic void Close (Messagecontext context) {} @Overridepublic Boolean Handlefault (Soapmessagecontext context) { return true;} @Overridepublic Boolean handlemessage (Soapmessagecontext context) {//Determines whether the message is input or output Boolean output = (Boolean) context.get (Messagecontext.message_outbound_property); System.out.println (output? "Response soap:": "Request soap:"); SoapMessage message = Context.getmessage (); try {Message.writeto (system.out);} catch (SoapException e) { E.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} System.out.println (""); return true;} @Overridepublic Set<qname> Getheaders () {return null;}} 

Build handler-chain.xml configuration file under Classpath

<?xml version= "1.0" encoding= "UTF-8"? ><javaee:handler-chains xmlns:javaee= "http://java.sun.com/xml/ns/ Java ee "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "><javaee:handler-chain><javaee:handler>< Javaee:handler-class>com.rvho.server.ws.handler.loggerhandler</javaee:handler-class></javaee: Handler></javaee:handler-chain></javaee:handler-chains>

To add a Handlerchain configuration on the service implementation class

Package Com.rvho.server.ws.impl;import Java.util.date;import Javax.jws.handlerchain;import javax.jws.WebService; Import Com.rvho.server.ws.HelloWService; @WebService (endpointinterface = "Com.rvho.server.ws.HelloWService", PortName = "Hellowsport", ServiceName = "Hellowsservice", targetnamespace = "Http://www.tmp.com/ws/hello") @ Handlerchain (file= "handler-chain.xml")//Add handler config file public class Hellowserviceimpl implements Hellowservice { Public String Index () {return "Hello";} Public integer Add (integer x, integer y) {return x + y;} Public date Now () {return new Date ();}}

Service Implementation Interface

Package Com.rvho.server.ws;import java.util.date;import javax.jws.webservice;/** * WebService interface */@WebService (name = " Hellows ", targetnamespace =" Http://www.tmp.com/ws/hello ") public interface Hellowservice {/** * return string *  * @return */ String index ();/** * Two integers added *  * @param x * @param y * @return Added value */integer Add (integer x, integer y);/** * return Current time *  * @return */date Now ();}

Client initiates index request, server-side record

Request Soap:<s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap-env= "http// schemas.xmlsoap.org/soap/envelope/"><soap-env:header/><s:body><ns2:index xmlns:ns2="/http Www.tmp.com/ws/hello "/></s:body></s:envelope> response soap:<s:envelope xmlns:s="/http/ schemas.xmlsoap.org/soap/envelope/"xmlns:soap-env=" http://schemas.xmlsoap.org/soap/envelope/"><soap-env: Header/><s:body><ns2:indexresponse xmlns:ns2= "Http://www.tmp.com/ws/hello" ><return>hello </return></ns2:indexResponse></S:Body></S:Envelope>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JAX-WS Handlerchain using the detailed

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.