Example of Jax-ws Handler

Source: Internet
Author: User

AOP is generally used for logging and authentication of function calls of an object.

WebService is a remote function call. Similar AOP methods are also required. For example, handler is equivalent to AOP for the WebService of Jax-ws.

 

Example of JAX-WS Handler

 

Write a WebService first

 Import javax. JWS. handlerchain; <br/> Import javax. JWS. webmethod; <br/> Import javax. JWS. webService; </P> <p> @ WebService <br/> @ handlerchain (file = "handlers. XML ") <br/> public class Hello {</P> <p> @ webmethod () <br/> Public String sayhello (string name) {<br/> return "hello" + name + ". "; <br/>}< br/>}

The only special WebService of a super dummies is the @ handlerchain annotation, in which handlers. XML is an XML file describing the handler chain of Jax-WS, which can be placed in the same directory as the source file.

Let's take a look at the content of handlers. xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <Handler-chains xmlns = "http://java.sun.com/xml/ns/javaee"> <br/> <Handler-Chain> <br/> <pandler> <br/> <Handler-Name> servicesoaphandler </Handler-Name> <br/> <Handler-class> COM. WS. handler. hellohandler </Handler-class> <br/> </pandler> <br/> </Handler-Chain> <br/> </Handler-chains>

The handler chain is defined, and there is only one handler in the chain. It can also contain multiple Handler

Let's look at the specific implementation of handler:

Import Java. util. set; </P> <p> Import javax. XML. namespace. QNAME; <br/> Import javax. XML. WS. handler. messagecontext; <br/> Import javax. XML. WS. handler. soap. soaphandler; <br/> Import javax. XML. WS. handler. soap. soapmessagecontext; </P> <p> public class hellohandler implements soaphandler <soapmessagecontext >{</P> <p> @ override <br/> Public Boolean handlemessage (soapmessagecontext context) {<br/> system. out. println (context. get (messagecontext. wsdl_service ). tostring (); <br/> return true; <br/>}</P> <p> @ override <br/> Public Boolean handlefault (soapmessagecontext context) {<br/> // todo auto-generated method stub <br/> return true; <br/>}</P> <p> @ override <br/> Public void close (messagecontext context) {<br/> // todo auto-generated method stub </P> <p >}</P> <p> @ override <br/> Public set <QNAME> getheaders () <br/>{< br/> // todo auto-generated method stub <br/> return NULL; <br/>}</P> <p>}

A handler must implement soaphandler or logicalhandler interface. For the differences between the two, the relationship with the handler interface, and the significance of xxxcontext in <>, see jaxws spec. Here is just a helloworld example.

I have actually implemented one method defined by all interfaces. I printed the name of the called WebService. In fact, I can do many things here, such as modifying the content in soap, add or delete XML tags, add or delete soap attachments, and obtain soap-related fields. For more image functions, you can add, decrypt, and log files.

Finally, modify the configuration file:

Add the following in Web. xml:

<listener> <br/> <listener-class> COM. sun. XML. WS. transport. HTTP. servlet. wsservletcontextlistener </listener-class> <br/> </listener> <br/> <servlet> </P> <p> <servlet-Name> wsservlet </servlet-name> <br/> <servlet-class> COM. sun. XML. WS. transport. HTTP. servlet. wsservlet </servlet-class> <br/> <load-on-startup> 1 </load-on-startup> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> wsservlet </servlet-Name> <br/> <URL-pattern>/helloservice </url-pattern> <br/> </servlet-mapping>

In fact, when there is a annotation marked with @ WebService, the container will automatically convert it into a WebService, but I have tried to use the above method, that is, when jaxws is run, wsservlet is used to match the request, and a class is specified to be loaded as WebService in the initial context through wsservletcontextlistener, this listener will automatically detect webroot (and web. the file named sun-jaxws.xml in the same directory of XML, which contains the specific implementation of WebService.

Here the sun-jaxws.xml content is:

<Endpoints <br/> xmlns = "http://java.sun.com/xml/ns/jax-ws/ri/runtime" <br/> Version = "2.0"> </P> <p> <endpoint <br/> name = "helloservice" <br/> Implementation = "com. WS. service. hello "<br/> URL-pattern ="/helloservice "/> </P> <p> </endpoints>

The specified implementation can be a class with @ WebService or a class that implements the provider interface. The relevant provider can view the official spec, which is the basis of Jax-Rs.

 

Package and deploy. I deployed it in glassfish. Find a WebService test tool. The eclipse Jee package contains the WebService client for testing.

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.