5. Create rest API in Spring boot

person shubham sharmafolder_openJAVA, Spring Bootlocal_offer, access_time November 17, 2024

Creating a REST API in Spring Boot is a straightforward process, thanks to Spring Boot’s powerful features and built-in support for RESTful services. In this guide, I will walk you through creating a simple REST API that interacts with a database (in-memory H2 database) to manage user data.

Steps to Create a REST API in Spring Boot

1. Set Up Spring Boot Project

You can set up a Spring Boot project using Spring Initializr, or directly in your IDE like IntelliJ or Eclipse.

Using Spring Initializr:
  • Visit start.spring.io
  • Choose:
    • Project: Maven Project
    • Language: Java
    • Spring Boot Version: 2.7.0 or later
    • Dependencies: Select the following dependencies:
      • Spring Web (for building REST APIs)
      • Spring Data JPA (for database access)
      • H2 Database (for in-memory database)
  • Generate the project and import it into your IDE.

Alternatively, you can add the dependencies manually in your pom.xml file:

2. Create Model (Entity)

Next, create a User model class that maps to a database table.

3. Create Repository

Create a UserRepository interface that extends JpaRepository, which provides built-in methods for CRUD operations.

4. Create Service Layer

Create a UserService class to handle the business logic.

5. Create REST Controller

Now, create a UserController to handle the API requests.

6. Configure H2 Database

To keep things simple, you can use H2 (an in-memory database) for testing and development. Add the following properties to application.properties to enable the H2 console and configure the database:

The H2 console will be accessible at http://localhost:8080/h2-console. You can use the JDBC URL jdbc:h2:mem:testdb to connect.

7. Run the Application

Once you have created the controller, service, and repository, you can run the Spring Boot application by executing the main method in your SpringBootApplication class.

Your REST API will now be running, and you can interact with it using a tool like Postman or curl.

Sample API Endpoints

  1. Get All Users:

    • GET /api/users
    • Response: A list of users.
  2. Get User by ID:

    • GET /api/users/{id}
    • Response: The user object if found, 404 otherwise.
  3. Create User:

    • POST /api/users
    • Request Body:
  4. Update User:

    • PUT /api/users/{id}
    • Request Body:
  5. Delete User:

    • DELETE /api/users/{id}
    • Response: 204 No Content if the user is deleted successfully.

Conclusion

In this guide, we walked through how to create a basic REST API in Spring Boot using Spring Data JPA and an in-memory H2 database. We defined a simple User entity, created a repository, service, and controller to expose CRUD operations. You can extend this by connecting to a more robust database (like MySQL or PostgreSQL), adding validation, security (using Spring Security), and more features depending on your application’s needs.

warningComments are closed.