Skip to content

ThomasWYang/candidate_spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Readme

This is a simple Rest Api created using Spring-Boot with in memory H2 database. The object Model is Candidate with fname(string), lname(string), email(string) and score(int). When the Api started, it will preloaded with 4 Candidates with predefined names and random scores from 60 to 100.

Original java code is tested under Windows environment. Docker file is tested under both Windows and EC2 linux environment.

Install via Git

  1. Make sure JDK/JRE 17+ (https://www.oracle.com/au/java/technologies/downloads/) and maven (https://maven.apache.org/download.cgi) are installed in the local environment. Use following command in command line to check.
java -version
mvn -version
  1. Use git clone to copy repo to local
git clone https://github.com/ThomasWYang/candidate_spring.git
  1. Change directory into that folder and run following command, the app will run on default port 8080.
./mvnw clean spring-boot:run

Or

mvn clean spring-boot:run

If you see this info is displayed, the app is running correctly 图片

Please refer to https://stackoverflow.com/questions/34253779/tomcat-server-error-port-8080-already-in-use if there is error mentioning that port 8080 already in use.

Test in local environment

Run the following command to execute Unit Test

./mvnw test

Or

mvn test

图片

Install via Docker

  1. Make sure Docker desktop (https://www.docker.com/) is running.
  2. Pull the docker image from docker hub.
docker pull thomasy2022/candidate_spring
  1. Run the following command to run the app in a container. The app will run on port 8080 with below command.
docker run -d -p 8080:8080 thomasy2022/candidate_spring
  1. You can also use docker-compose.yml file inside this repo combined with the docker image to run the app
docker compose up

It will run on configured port 8080 and you will see similar info below. 图片

Run the app directly from instance deployed in AWS EC2

Try http://13.236.184.156/candidates in browser. Replace "localhost:8080" with "13.236.184.156" for all the command below if you wish to test the api deployed in AWS EC2.

curl -v "13.236.184.156/candidates/scoregreater/90"

Usage

You may test the script via various tools like browsers and postman, I will show how to use curl to test the api in windows cmd or linux below. (add -v after curl to show detailed info)

  1. Show all candidates
curl -v "localhost:8080/candidates"
  1. Show single candidate by ID, if exist, that candidate will be returned.
curl "localhost:8080/candidates/1"
  1. Show single candidate by ID, if not exist, Not found info will be returned.
curl "localhost:8080/candidates/99"
  1. Search candidates by fname, lname or email (you can give any combination of these 3 fields).
curl “localhost:8080/candidates?fname=Thomas&lname=Yang”
curl “localhost:8080/candidates?[email protected]
  1. Search candidates by minimum score
curl “localhost:8080/candidates/scoregreater/80"
  1. Create candidate with fname, lname and email (id will be generated automatically)
curl -v -X POST "localhost:8080/candidates" -H "Content-type:application/json" -d "{\"fname\": \"aaa\", \"lname\": \"bbb\", \"email\":\"[email protected]\"}"
  1. Create candidate with fname, lname, email and score (id will be generated automatically)
curl -v -X POST "localhost:8080/candidates" -H "Content-type:application/json" -d "{\"fname\": \"mmm\", \"lname\": \"nnn\", \"email\":\"[email protected]\", \"score\":80}"
  1. Update candidate with id
curl -v -X PUT "localhost:8080/candidates/4" -H "Content-type:application/json" -d "{\"fname\": \"xxx\", \"lname\": \"yyy\", \"email\":\"[email protected]\", \"score\":90}"
  1. Delete candidate with id
curl -v -X DELETE "localhost:8080/candidates/4"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published