Develop Java API Running On Tomcat Server

How To Develop And Deploy Your Web App (Angular + Tomcat Server + MySQL) — Part 3

Mia
5 min readJul 26, 2020

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)

Pikachu In Nara (By Mia)
  • 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

Create new project with Intellij IDEA
New project settings in Intellij IDEA
New project settings in Intellij IDEA

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…

Add maven support settings
Add Frameworks Support

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

Pom.xml

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.

pom.xml

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

web.xml

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

Load maven changes to fix the error

Write A Simple RESTful API

Create a Hello.java file under ws package. First, create a ws package (ws stands for Web Service).

Create ws package
Create Hello.java under ws package
Instert name and press Enter to create

Add the following code into Hello.java.

Hello.java

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

Hello.java

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.

Edit Configuration

Create and config a local Tomcat server:

Run/Debug Configurations
Run/Debug Configurations

Notes: We use this URL to access the Tomcat Server.

Request URL

Artifacts configurations:

File -> Project Structure
Project Structure (Choose From Modules)
Select Modules

Input a name you like, others remain by default. Make sure all libraries are included.

Config Artifacts Settings

Notes:

  • Input a name for your Artifacts.
  • Output directory can use default setting.
  • If there are still libraries in TomcatTest move them to Output Root folder.
Project Structure -> Artifacts

Build Artifacts:

Build -> Build Artifact
LocalTomcat -> Build

Wait for a while, then click Run to deploy this program on local Tomcat.

Run this program

Tomcat starts successfully:

Starts successfully

Check your api function:

API responses HTML page

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 /.

web.xml
Hello.java

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.

Test Request using 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.

References

--

--