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.
- 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
- Use git clone to copy repo to local
git clone https://github.com/ThomasWYang/candidate_spring.git
- 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.
Run the following command to execute Unit Test
./mvnw test
Or
mvn test
- Make sure Docker desktop (https://www.docker.com/) is running.
- Pull the docker image from docker hub.
docker pull thomasy2022/candidate_spring
- 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
- 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.
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"
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)
- Show all candidates
curl -v "localhost:8080/candidates"
- Show single candidate by ID, if exist, that candidate will be returned.
curl "localhost:8080/candidates/1"
- Show single candidate by ID, if not exist, Not found info will be returned.
curl "localhost:8080/candidates/99"
- 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]”
- Search candidates by minimum score
curl “localhost:8080/candidates/scoregreater/80"
- 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]\"}"
- 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}"
- 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}"
- Delete candidate with id
curl -v -X DELETE "localhost:8080/candidates/4"