Developing web services with JAX-WS

JAX-WS is a high-level API that simplifies the development of SOAP-based web services. JAX-WS stands for Java API for XML Web Services. Developing a web service via JAX-WS consists of writing a class with public methods to be exposed as web services. The class needs to be decorated with the @WebService annotation. All public methods in the class are automatically exposed as web services. They can optionally be decorated with the @WebService annotation. The following example illustrates this process:

package net.ensode.glassfishbook; 
 
import javax.jws.WebMethod; 
import javax.jws.WebService; 
 
@WebService 
public class Calculator { 
 
    @WebMethod 
    public int add(int first, int second) { 
        return first + second; 
    } 
 
    @WebMethod 
    public int subtract(int first, int second) { 
        return first - second; 
    } 
} 

The preceding class exposes its two methods as web services. The add() method simply adds the two int primitives it receives as parameters and returns the result; the substract() method subtracts its two parameters and returns the result.

We indicate that the class implements a web service by decorating it with the @WebService annotation. Any methods that we would like to expose as web services can be decorated with the @WebMethod annotation, but this isn't necessary; all public methods are automatically exposed as web services. We can still use the @WebMethod annotation for clarity, but it isn't strictly necessary to deploy our web service; we simply need to package it in a WAR file as usual.

Web service clients need a WSDL (Web Services Definition Language) file in order to generate executable code that they can use to invoke the web service. WSDL files are typically placed in a web server and accessed by the client via its URL.

When deploying web services developed using JAX-WS, a WSDL is automatically generated for us. The exact URL for the generated WSDL varies depending on the Java EE 8 application server we are using. When using GlassFish, URLs for JAX-WS WSDLs follow the following format:

[http|https]://[server]:[port]/[context root]/[service name]?wsdl

In our example, the URL for our web service's WSDL (when deployed to GlassFish) would be http://localhost:8080/calculatorservice/CalculatorService?wsdl (assuming GlassFish is running on our local workstation, and GlassFish is listening for HTTP connections on its default 8080 port).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.151.44