The actual application case of JAKARTA XML Web Services API in the Java class library
Jakarta XML Web Services API (referred to as JAX-WS) is a powerful tool in the Java class library for the development of web services.It provides APIs for creating and deploying Web services, supporting Soap (Simple Object Access Protocol) and WSDL (Web Services Description Language).JAX-WS allows Java applications to communicate as web service providers or consumers to communicate by using standard XML messages.
Below is a practical application case to demonstrate how to use JAX-WS to create and deploy a simple web service.
Suppose we want to implement a simple calculator web service, which will provide basic mathematical computing functions, such as addition and multiplication.We will create a class called Calculator and use JAX-WS to expose it to a web service.
First, we need to create a Calculator class in Java, which will achieve our business logic.The following is a simple example:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
}
Next, we need to use the JAX-WS annotation to mark our Calculator class and define the way to make public.In this class, we use the@webservice` annotation, and use the@webmethod` annotation in the public method.The following is the code after modification:
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class Calculator {
@WebMethod
public int add(int a, int b) {
return a + b;
}
@WebMethod
public int multiply(int a, int b) {
return a * b;
}
}
Now, we are ready to deploy the Calculator class as a web service.To this end, we need to use the tools provided by JAX-WS to generate WSDL documents and other deployment files.
In the command line, we can use the following command to generate WSDL documents:
wsgen -classpath <classpath> -keep -wsdl <package_name>.<Calculator_class_name>
In the above commands, `ClassPath>` is the class path where the Calculator class is located.
After running the above commands, WSDL documents and other related files will be generated.
Finally, we can deploy the generated files on a Java application server, such as Apache Tomcat.After the deployment is completed, we can access the public method of the Calculator class through the URL address of the web service.
For example, suppose we deploy web services in `http:// localhost: 8080/Calculator`.We can use the following Java code to call the ADD method of the Calculator class:
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
public class CalculatorClient {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/Calculator?wsdl");
QName qname = new QName("http://webservice.package", "CalculatorService");
Service service = Service.create(url, qname);
Calculator calculator = service.getPort(Calculator.class);
int result = calculator.add(5, 10);
System.out.println("Result: " + result);
}
}
In the above code, we first create a URL object, which specifies the WSDL address of the web service.Then, we specified the name and service name of the WebService via Qname and created a service object.Finally, we obtain the proxy object of the Calculator class by calling the service.getport method, and then we can call the public method.
Through the above example, we can see that JAX-WS provides a simple and powerful way to create and deploy Web services and interact with it.Whether in enterprise-level applications or in mobile applications, JAX-WS can help us build flexible and scalable web services.