SoFunction
Updated on 2025-03-08

Detailed explanation and simple examples of jax-ws handler

Detailed explanation and simple examples of jax-ws handler

aop technology is generally used for logging, authentication, etc. of a certain object's function call.

Webservice is a remote function call, and a similar AOP method is also required. For example, a jax-ws webservice, handler is equivalent to AOP.

Take a jax-ws handler example

Write a website first

import ; 
import ; 
import ; 
 
 
@WebService 
@HandlerChain(file="") 
public class Hello { 
 
  @WebMethod() 
  public String sayHello(String name) { 
    return "Hello " + name + "."; 
  } 
} 

The only special thing about the super fool's webservice is the @HandlerChain annotation, which is an xml file describing the handler chain of jax-ws. This file can be placed in the same directory as this source file.

Take a look at the content

<?xml version="1.0" encoding="UTF-8"?> 
<handler-chains xmlns="/xml/ns/javaee"> 
  <handler-chain> 
    <handler> 
      <handler-name>ServiceSOAPHandler</handler-name> 
      <handler-class></handler-class> 
    </handler> 
  </handler-chain> 
</handler-chains> 

The handler chain is defined, there is only one handler in the chain, and it can also include multiple handlers.

Let’s take a look at the specific implementation of handler:

import ; 
 
import ; 
import ; 
import ; 
import ; 
 
public class HelloHandler implements SOAPHandler<SOAPMessageContext> { 
 
 
  @Override 
  public boolean handleMessage(SOAPMessageContext context) { 
    ((MessageContext.WSDL_SERVICE).toString()); 
    return true; 
  } 
 
  @Override 
  public boolean handleFault(SOAPMessageContext context) { 
    // TODO Auto-generated method stub 
    return true; 
  } 
 
  @Override 
  public void close(MessageContext context) { 
    // TODO Auto-generated method stub 
     
  } 
 
  @Override 
  public Set<QName> getHeaders() 
  { 
    // TODO Auto-generated method stub 
    return null; 
  } 
 
} 

A handler must implement the SOAPHandler or LogicalHandler interface. As for the difference between them, the relationship with the Handler interface, and the meaning of XXXContext in <>, please refer to the spec of jaxws, here is just a helloworld example.

I actually implemented one of the methods defined by all interfaces, printing the name of the called webservice. In fact, I can do a lot of things here, such as modifying the content in soap, adding or deleting xml tags, adding and deleting soap attachments, getting soap related fields, and more vivid functions include encryption and decryption, logs, etc.

Finally, some related configuration file modifications need to be made:

Add in:

<listener> 
    <listener-class></listener-class> 
  </listener> 
  <servlet> 
     
    <servlet-name>wsservlet</servlet-name> 
    <servlet-class></servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>wsservlet</servlet-name> 
    <url-pattern>/helloservice</url-pattern> 
  </servlet-mapping> 

In fact, when a class is marked with @webservice, the container will automatically turn it into a webservice, but I have tried this method here, that is, use the jaxws runtime to match the request with a wsservlet, and before, it used the wsservlet context listener to specify the loading of a certain class as a webservice in the initial context. This listener will automatically detect the file named in webroot (and the same directory), which contains the specific implementation of the webservice.

The content here is:

<endpoints 
  xmlns="/xml/ns/jax-ws/ri/runtime" 
  version="2.0"> 
 
  <endpoint 
    name="helloservice" 
    implementation="" 
    url-pattern="/helloservice" /> 
 
</endpoints> 

The implementation specified in this 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.

Packaging and deployment, I deployed it in glassfish, and found a webservice test tool. The eclipse jee package has its own webservice client test.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!