Develop Java API Running On Tomcat Server
How To Develop And Deploy Your Web App (Angular + Tomcat Server + MySQL) — Part 3
Part 1: Develop First Web Page With Angular
Part 2: Install Tomcat On Server (Mac & Linux)
Part 3: Develop Java API Running On Tomcat Server
Part 4: Install MySQL And Create Database (Local & Remote Server)
Part 5: Connect To MySQL Database in Java API and Deploy It On Tomcat Server (Local Server & Remote Server)

- Introduction
- Prerequisites
- Develop Java API Program
- References
Introduction
In this article, I will show you how to write a Java RESTful API program and run it on Tomcat server.
Prerequisites
- A Linux server: CentOS 7 (How to own your VPS? Check my another article to Setup your VPS)
- My local computer is MacOS
- Intellij IDEA (Code editor for JAVA API)
- Java on remote server (Java version: 1.8)
- Tomcat on remote server (Tomcat version: 7.0.76)
- MySQL on remote server (MySQL version: 5.5.62)
- [Optional] Postman (Help me test APIs easily)
Note: You can choose other code editors or version you like. Those just for your reference.
Develop Java API Program
Once you have done the previous parts, you can create a Java API project with Intellij IDEA. This API program will run on the Tomcat server and manage requests from frontend. Here is a simple tutorial.
Create A New Project



Note: Just modify the Project name and other settings will automatically change.
After the project is created, we use Add Frameworks Support to introduce support for the maven project.
Right click TomcatTest folder, thenchoose Add Framework Support…


Then it will generate a pom.xml file. We will add our project dependencies in this file.

Configure Environment Settings
Modify pom.xml to introduce Jersey-related jar packages. (What is Jersey & Why?)
[IMPORTANT]: Make sure to click the maven button to load maven changes.

Add the following code into web.xml file ( param-value
should be the package name.)

If error prompts, click the maven button to load maven changes.

Write A Simple RESTful API
Create a Hello.java file under ws package. First, create a ws package (ws stands for Web Service).



Add the following code into Hello.java.

If errors prompt, follow the instructions to import the packages.

Notes: More explainations about the code, please refer to this tutorial.
Test RESTful API On Tomcat
You can get page from Java API. If your Run
button is disabled, press Edit Configuration
to set up your Tomcat.
Create and config a local Tomcat server:
Notes: We use this URL to access the Tomcat Server.
Artifacts configurations:
Input a name you like, others remain by default. Make sure all libraries are included.
Notes:
- Input a name for your Artifacts.
Output directory
can use default setting.- If there are still libraries in
TomcatTest
move them toOutput Root
folder.
Build Artifacts:
Wait for a while, then click Run
to deploy this program on local Tomcat.
Tomcat starts successfully:
Check your api function:
Notes: Why is /api/hello
? The JAX-RS Servlet will response the client’s
request according to the URL. Requests can be something else, just change <url-pattern>
. It’s better to set under /path/*
instead of /
.
Leave a small question here:
When we use browser to test http://localhost:8080/TomcatTest_war_exploded/api/hello request, it will display the result correctly. But it will return 406 error if we use Postman.
Reason: There’s a CORS issue and I will show you how to solve this problem in Part 4.
More…
The following parts could be found in my other articles.
- Part 1: Develop First Web Page With Angular
- Part 2: Install Tomcat On Server (Mac & Linux Server)
- Part 3: Develop Java API Program Running On Tomcat Server
- Part 4: Install MySQL And Create Database (Local & Remote Server)
- Part 5: Connect To MySQL Database in Java API and Deploy It On Tomcat Server (Local Server & Remote Server)