#
Create a simple REST API
This tutorial explains to you how to create a simple REST API in Spring Boot.
There are many ways you can get started with a Spring Boot application. One of them is to use Spring initializr .
When this is done we need to add code to that application, which has no functionalities.
Here are the classes you can add in the order to have a functional Spring API:
Employee.java
package com.example.restdemo.employee;
public class Employee {
private String id;
private String name;
private String department;
private int age;
public Employee(String id, String name, String department, int age) {
this.id = id;
this.name = name;
this.department = department;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
EmployeeService.java
package com.example.restdemo.employee;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
@Service
public class EmployeeService {
private HashMap<String, Employee> employees = new HashMap<>();
public void addEmployee(Employee Emp) {
this.employees.put(Emp.getId(), Emp);
}
public void removeEmployee(String empId) {
this.employees.remove(empId);
}
public int countEmployees() {
return this.employees.size();
}
public ArrayList<Employee> getEmployees() {
ArrayList<Employee> empList = new ArrayList<>();
// go through the values of the map
for (Employee emp : employees.values()) {
empList.add(emp);
}
return empList;
}
}
EmployeeController.java
package com.example.restdemo.employee;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/employee/all")
String getAllEmployees() {
ArrayList<Employee> allEmployees = employeeService.getEmployees();
String json = new Gson().toJson(allEmployees);
return json;
}
@PutMapping(value="/employee/add", consumes = "application/json")
int addEmployee(@RequestBody Employee newEmployee) {
employeeService.addEmployee(newEmployee);
int newCount = employeeService.countEmployees();
return newCount;
}
@DeleteMapping(value="/employee/delete", consumes = "application/json")
int deleteEmployee(@RequestBody String empId) {
employeeService.removeEmployee(empId);
int newCount = employeeService.countEmployees();
return newCount;
}
}
Also, in pom.xml you need to add the following dependency:
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
If you have Maven installed you can create the executable jar file by running one of the commands:
mvn install
mvn clean install
: in addition tomvn install
it cleans up anything that was created by previous builds
Info
- "clean" is a phase of the clean lifecycle
- "install" is a phase of the default lifecycle
Now you can run the Spring Boot application by running the command java -jar target/rest-demo-v1.jar
:
java -jar target/rest-demo-v1.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.1)
2023-01-07T20:57:50.440+02:00 INFO 2440 --- [ main] c.example.restdemo.RestDemoApplication : Starting RestDemoApplication vv1 using Java 19.0.1 with PID 2440 (D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\rest-demo-v1.jar started by Catalin in D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo)
2023-01-07T20:57:50.443+02:00 INFO 2440 --- [ main] c.example.restdemo.RestDemoApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-07T20:57:51.232+02:00 INFO 2440 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-01-07T20:57:51.241+02:00 INFO 2440 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-07T20:57:51.241+02:00 INFO 2440 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-07T20:57:51.311+02:00 INFO 2440 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-01-07T20:57:51.312+02:00 INFO 2440 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 820 ms
2023-01-07T20:57:51.656+02:00 INFO 2440 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-01-07T20:57:51.671+02:00 INFO 2440 --- [ main] c.example.restdemo.RestDemoApplication : Started RestDemoApplication in 1.576 seconds (process running for 1.925)
Info
There are three built-in build lifecycles: default, clean and site:
- the default lifecycle handles your project deployment
- the clean lifecycle handles project cleaning
- the site lifecycle handles the creation of your project's website