Welcome to the comprehensive guide on creating RESTful API with Java Spring Boot. In this article, we will see how to create an API using Java, MySQL, Spring Boot, JPA, and Maven. We will also perform some CRUD operations to add, modify, and delete data from the database. This guide will help you with the knowledge to develop efficient and scalable APIs for your Java projects.
Create a New Spring Starter App
Create a New Spring Starter Project. Here we give the name of the project as Rest-API. Select the type as Maven. I'm choosing the java version 17 for this project. For the package select jar and for the artifact you can have your name or simply mention it as a project. You can add a description of your own and it is optional. Click Next.

Select the dependencies for the project. I'm using Spring Boot version 3.2.5 for this project. Since we are using MySQL for the database and it is a web project, choose the dependencies MySQL Driver, Spring Web, and Spring Data JPA(Java Persistence API).

Clicking Finish will create a new project with all the dependencies selected. You can check the dependencies in the pom.xml file.
Create User Model Class
Create a user model class and add the following code.
package com.dhan.Models;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private long id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private int age;
@Column
private String occupation;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
}
- @Entity: Indicates that this class is a JPA entity, meaning it is mapped to a database table. Each instance of this class represents a row in the table.
- @Column: Specifies the mapping of the annotated field to the corresponding column in the database table.
- @Id: Indicates the primary key of the entity.
Create an API controller class
Create a package called controller and create a new class inside of the package. Add the following code to the controller.
package com.dhan.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.dhan.Models.User;
import com.dhan.Repo.UserRepo;
@RestController
public class ApiControllers {
@Autowired
private UserRepo userRepo;
@GetMapping("/")
public String page() {
return "Welcome";
}
@GetMapping("/users")
public List<User> getUsers(){
return userRepo.findAll();
}
@PostMapping("/save")
public String saveUsers(@RequestBody User user) {
userRepo.save(user);
return "Saved...";
}
@PutMapping("update/{id}")
public String updateUser(@PathVariable long id, @RequestBody User user) {
User updatedUser = userRepo.findById(id).get();
updatedUser.setFirstName(user.getFirstName());
updatedUser.setLastName(user.getLastName());
updatedUser.setOccupation(user.getOccupation());
updatedUser.setAge(user.getAge());
userRepo.save(updatedUser);
return "Updated... ";
}
@DeleteMapping("/delete/{id}")
public String deleteUser(@PathVariable long id) {
User deleteUser = userRepo.findById(id).get();
userRepo.delete(deleteUser);
return "Deleted user with id " + id;
}
}








Join the conversation! Your thoughts help the community grow.