0

I'm in a tricky situation, I'm mainly a frontend dev working in react but I got into a project where i need to create an API with some endpoints in various languages. I have done Python and dot Net already. Now it's time for Java. I'm totally new to all the backend languages except nodejs so sorry if I'm incorrect on some things.

I've set up a Java project with a Tomcat web server. I can run it locally on port 8080. I want to create a simple API with 3 endpoints that only returns some text. What is the simplest way of doing this? Thank you!

gospecomid12
  • 712
  • 3
  • 11
  • 25

1 Answers1

0

You can use Spark (https://sparkjava.com/) for that purposes. Its API is similar to Node JS ones. And you can deploy it to the Tomcat.

BTW, Tomcat is the envirenment for running Java applications. It is like Node JS.

mr.M
  • 851
  • 6
  • 23
  • 41
  • Thank you! It looks real simple. Can i do this in webapps/app/WEB-INF/classes directory? I dont understand much about the structure since I'm not going to work with Java in the future. – gospecomid12 Jul 12 '21 at 11:51
  • One more question, do I need to download Maven for this to work? – gospecomid12 Jul 12 '21 at 11:57
  • It can work with either with Maven or Gradle. Take a look at documentation: https://sparkjava.com/documentation#routes – mr.M Jul 12 '21 at 12:13
  • Ok, do i run the server from apache-tomcat directory or spark directory in the root? Or maybe I need to put the spark dir into the apache-tomcat? And where should I create the HelloWorld.java file? I dont understand, sorry – gospecomid12 Jul 12 '21 at 12:23
  • Create file, Main.java. Write main method, then copy paste the sample, then compile it, and then run it (or, scip compiling and run it from IDE). Here is how you deploy it to Tomcat https://stackoverflow.com/questions/19783646/how-to-deploy-a-spark-java-web-app – mr.M Jul 12 '21 at 12:36