Spring Boot REST API CRUD Example 1
Spring Boot REST API CRUD Example 1
Check out my 10+ Udemy bestseller courses and discount coupons: Udemy Courses - Ramesh Fadatare
Java JavaEE Library REST Spring Boot Microservices Full Stack YouTube UI Quiz Hibernate DB Programs
Kotlin Python Me
This tutorial will teach you how to build CRUD REST APIs using Spring Boot 3, Spring Data JPA, and MySQL Database.
We’ll first build the APIs to create, retrieve, update and delete a user, then test them using postman.
Note that we are using the latest version of Spring boot which is version 3.
Refer to the below screenshot to enter details while creating the spring boot application using the spring initializr:
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 1/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
Click on Generate button to download the Spring boot project as a zip file. Unzip the zip file and import the Spring boot project
in IntelliJ IDEA.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 2/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
<groupId>org.projectlombok</groupId>
Tutorials Guides Annotations Interview Quizzes
Java Guides <artifactId>lombok</artifactId>
YouTube Udemy MCQs SCE
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2. Project Structure
Refer to the below screenshot to create a project structure or a packing structure for our Spring boot application:
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 3/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
spring.datasource.url=jdbc:mysql://localhost:3306/user_management
spring.datasource.username=root
spring.datasource.password=Mysql@123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
Don’t forget to change the spring.datasource.username and spring.datasource.password as per your MySQL installation.
Also, create a database named user_management in MySQL before proceeding to the next section.
You don’t need to create any tables. The tables will automatically be created by Hibernate from the User entity that we will
define in the next step. This is made possible by the property spring.jpa.hibernate.ddl-auto = update.
Let's create a User JPA entity class with the following fields:
id - primary key
firstName - user first name
lastName - user last name
email - user email ID
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 4/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false, unique = true)
private String email;
}
Note that we are using Lombok annotations to reduce the boilerplate code such as getter/setter methods, and constructors.
We are using below JPA annotations to map an Entity with a database table:
@Table annotation is used to provide the details of the table that this entity will be mapped to.
@GeneratedValue annotation is used to define the primary key generation strategy. In the above case, we have declared the
@Column annotation is used to define the properties of the column that will be mapped to the annotated field. You can define
Well, Spring Data JPA comes with a JpaRepository interface that defines methods for all the CRUD operations on the entity,
package net.javaguides.springboot.repository;
import net.javaguides.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 5/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
UserService Interface
package net.javaguides.springboot.service;
import net.javaguides.springboot.entity.User;
import java.util.List;
List<User> getAllUsers();
UserServiceImpl Class
package net.javaguides.springboot.service.impl;
import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.User;
import net.javaguides.springboot.repository.UserRepository;
import net.javaguides.springboot.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
@Override
public User createUser(User user) {
return userRepository.save(user);
}
@Override
public User getUserById(Long userId) {
Optional<User> optionalUser = userRepository.findById(userId);
return optionalUser.get();
}
@Override
public List<User> getAllUsers() {
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 6/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
return userRepository.findAll();
Tutorials Guides Annotations Interview Quizzes
Java Guides }
YouTube Udemy MCQs SCE
@Override
public User updateUser(User user) {
User existingUser = userRepository.findById(user.getId()).get();
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.setEmail(user.getEmail());
User updatedUser = userRepository.save(existingUser);
return updatedUser;
}
@Override
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
}
}
Let's create the REST APIs for creating, retrieving, updating, and deleting a User :
package net.javaguides.springboot.controller;
import lombok.AllArgsConstructor;
import net.javaguides.springboot.entity.User;
import net.javaguides.springboot.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@AllArgsConstructor
@RequestMapping("api/users")
public class UserController {
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 7/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
// Build Get All Users REST API
Tutorials Guides Annotations Interview Quizzes
Java Guides // http://localhost:8080/api/users
YouTube Udemy MCQs SCE
@GetMapping
public ResponseEntity<List<User>> getAllUsers(){
List<User> users = userService.getAllUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
container(embedded tomcat).
1. From the root directory of the application and type the following command to run it -
$ mvn spring-boot:run
2. From your IDE, run the SpringbootRestfulWebservicesApplication.main() method as a standalone Java class that will start the
embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 8/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 9/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 10/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
10. Conclusion
Congratulations guys! We successfully built a Restful CRUD API using Spring Boot 3, Spring Data JPA, and MySQL database.
Spring Boot Kafka Microservices Spring Boot + Apache Kafka Tutorial Spring Core Tutorial
Spring MVC Tutorial Spring Data JPA Tutorial Spring Framework For Beginners Spring AOP Tutorial
Spring Security Tutorial Spring Exceptions Tutorial Spring Boot Interview Questions
Spring Boot Microservices Interview Questions Apache Kafka Tutorials Docker Tutorials And Guides
Spring Boot RabbitMQ Tutorials Angular CRUD Example With Spring Boot
Spring Boot + Angular 12 CRUD Full Stack Spring Boot + Angular 8 CRUD Full Stack
Spring Boot + Angular 10 CRUD Full Stack Spring Boot + React JS CRUD Full Stack
React JS ( React Hooks) + Spring Boot Spring Boot Thymeleaf CRUD Full Stack
Spring Boot User Registration And Login Node Js + Express + MongoDB CRUD
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 11/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
YouTube 130K
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 12/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 13/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
About Me
Follow Me on Twitter
Follow
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 14/15
12/9/23, 10:48 AM Spring Boot REST API CRUD Example With MySQL Database
Learn Spring Boot 3 Learn Java 8 Java 8 Quiz
Tutorials Guides Annotations Interview Quizzes
Java Guides
Learn Spring MVC Learn Spring Boot Java Coding Quiz
YouTube Udemy MCQs SCE
Learn Spring Data JPA Learn Spring Framework Spring Boot Quiz
Learn Spring Boot React Learn Spring MVC Spring Quiz
Learn Java Collections Learn Spring Security Spring MVC Quiz
Learn Java 8 in 4 Hours Learn Spring Data JPA Spring Data JPA Quiz
Learn 25+ Spring Boot Annotations Learn Spring Annotations Hibernate Quiz
Spring Boot Kafka Microservices Learn Microservices Miroservices Quiz
Learn Building Spring Boot Projects Learn REST API REST API Quiz
Learn Spring Boot REST API Learn JPA JavaScript Quiz
Learn Event-Driven Microservices Learn Hibernate ORM JavaScript Coding Quiz
Learn Spring Boot + Kafka Learn JSP React JS Quiz
Learn Spring Boot + Angular Learn Servlet Kotlin Quiz
Learn Spring Boot + Thymeleaf Learn JUnit SQL Quiz
Learn Thymeleaf CSS Quiz
JSON Quiz
Copyright © 2018 - 2025 Java Guides All rights reversed | Privacy Policy | Contact | About Me | YouTube | GitHub
Powered by Blogger
https://www.javaguides.net/2022/09/spring-boot-rest-api-crud-example-with-mysql-database.html 15/15