I am learning about webservices, and I've done a simple Calculator application using webservices, where the Calculator and its functions to perform the operations are in a "server" class, and the interface (the client) is in another project.
The server is as follows:
package webservice.server;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class Calculator {
public static void main(String[] args) {
System.out.println("Server started at: http://localhost:12345/calc");
Endpoint.publish("http://localhost:12345/calc", new Calculator());
}
public int compute(int x, int y, String operation) {
return Calculator.calculate(x, y, operation);
}
public static int calculate(int x, int y, String operation) {
int result;
String op;
if ("ADD".equals(operation)) {
result = x + y;
op = "+";
} else if ("SUB".equals(operation)) {
result = x - y;
op = "-";
} else if ("MULT".equals(operation)) {
result = x * y;
op = "*";
} else if ("DIV".equals(operation)) {
result = x / y;
op = "/";
} else {
// defaults to SUB
result = x - y;
op = "-";
}
log(x, y, result, op);
return result;
}
private static void log(int x, int y, int result, String op) {
System.out.format("%d %s %d = %d%n", x, op, y, result);
}
}
With this server running, I run the following command in the Terminal if the IntelliJ IDEA IDE I'm using:
wsimport -keep -p webservice.client http://localhost:12345/calc?wsdl
, to create the client files, creating automatically the following structure (I'm using IntelliJ Community):
Currently, I am able to start my Calculator class (my server), and after I can start my CalculatorClient class to perform the calculations I need. Here is the code of this client:
package webservice.client;
public class CalculatorClient {
/**
* Starts the web service client.
*/
public static void main(String[] args) {
CalculatorService client = new CalculatorService();
Calculator calculatorService = client.getCalculatorPort();
int result = calculatorService.compute(10, 20, "ADD");
System.out.println("Returned value from server: " + result);
}
}
With the following result:
Returned value from server: 30
Process finished with exit code 0
As you can see, the Calculator client sends two values and the operation to the server via the webservice created, and the server returns the value to the client, so it can be printed.
This runs perfectly, so now my question is: How can I add this Calculator server class to an Apache Tomcat service? Is that possible? I've downloaded and installed the Apache Tomcat to my PC to learn how can I create webservices like this, just functionalities that will be accessed from another client program, as the CalculatorClient does, and these functionalities will be running on a local PC.
In my Apache Tomcat server I can see the following:
How can I add my Calculator class (the server) here and start it or stop it as needed, so my CalculatorClient can access to it? Is that possible? If not, how can I create services in Java like my Calculator class and add them to this Apache Tomcat server?
Thanks for any help or suggestion!
Daniel.
question from:
https://stackoverflow.com/questions/65933301/adding-a-java-class-as-a-webservice-to-the-apache-tomcat-server-to-call-it-fro