Then in an article, here is an example of a client-side handler.
First, put the WebService service-side code on the whole.
WebService
Import Javax.jws.HandlerChain; Import Javax.jws.WebMethod; Import Javax.jws.WebService; @WebService @HandlerChain (file= "Handlers.xml") public class Hellows {@WebMethod public string SayHello (string name) {RET Urn "Hello" + name + ".";}}
Handler
Import Java.util.Set; Import Javax.xml.namespace.QName; Import Javax.xml.ws.handler.MessageContext; Import Javax.xml.ws.handler.soap.SOAPHandler; Import Javax.xml.ws.handler.soap.SOAPMessageContext; public class Hellohandler implements soaphandler<soapmessagecontext> {@Override public boolean handlemessage ( Soapmessagecontext context) {System.out.println (Context.get (Messagecontext.wsdl_service). toString ()); @Override public boolean Handlefault (Soapmessagecontext context) {//TODO auto-generated method stub return true;} @Over Ride public void Close (Messagecontext context) {//TODO auto-generated ' stub} @Override public set<qname> ge Theaders () {//TODO auto-generated method stub return null;}}
Here we use J2SE to publish WebService, the main function is:
Import Javax.xml.ws.Endpoint; public class Servermain {/** * @param args */public static void main (string[] args) {endpoint.publish ("http://localhost : 8000/hellows3/hellowsservice ", New Hellows ()); } }
To run the main function, you can publish the WEBSERVICE,WSDL address: http://localhost:8000/HelloWS3/HelloWSService?wsdl
Generating client code
Generating client code with the Wsimport command
The command is: ... /workspace/hellows3client/bin>wsimport-keep http://localhost:8000/HelloWS3/HelloWSService?wsdl-p Com.ws.client
Where keep represents saving the source file,-p indicates that the code is saved under the specified package.
Then create a new client main function under Com.ws.client, where handler is inserted into the client's WebService proxy object, to facilitate the use of the same handler class above, the client-side code is as follows:
Import java.util.List; Import javax.xml.ws.Binding; Import Javax.xml.ws.BindingProvider; Import Javax.xml.ws.handler.Handler; Import Com.ws.handler.HelloHandler; public class Clientmain {/** * @param args */public static void main (string[] args) {Hellowsservice hs=new Hellowsservi CE (); Hellows hello= Hs.gethellowsport (); Binding binding= ((bindingprovider) hello). getbinding (); List
The purpose of the code is to add a handler to the Handlerchain of the Bindingprovider object.
Keep the previous server WebService running, then run this client, and you can print the WebService name of the call in the console of the two process.
Server
2011-5-4 18:12:08 Com.sun.xml.internal.ws.model.RuntimeModeler Getrequestwrapperclass
Info: Dynamically creating request wrapper Class Com.ws.service.jaxws.SayHello
2011-5-4 18:12:08 Com.sun.xml.internal.ws.model.RuntimeModeler Getresponsewrapperclass
Info: Dynamically creating response wrapper bean Class Com.ws.service.jaxws.SayHelloResponse
{http://service.ws.com/} Hellowsservice
{http://service.ws.com/} Hellowsservice
Client
{http://service.ws.com/} Hellowsservice
{http://service.ws.com/} Hellowsservice
Hello XXXXXXX.S