api-library

Spring Boot - CRUD REST API

Author: Jordy Hamwijk
Created: 2024-04-05
Last updated: 2024-05-17</br>

1. What is a CRUD REST API?

A CRUD REST API allows clients to perform basic operations on resources via HTTP requests. These operations include creating new resources, reading existing ones, updating resource data, and deleting resources. It adheres to the principles of REST, ensuring a predictable and uniform interface for communication between clients and servers.</br>

CRUD stands for Create, Read, Update, and Delete. </br> It represents the four fundamental operations used to interact with database applications.
These operations allow developers to manipulate data within a collection or database. Here’s what each function does:

2. How a CRUD REST API works in Spring Boot?

Spring Boot simplifies building RESTful APIs by providing a framework for handling HTTP requests and integrating with databases.</br>

2.1 Create a Spring Boot Application

Go to Spring and create a new Spring Boot project.

01-start-spring-io

Add additional dependencies:

ModelMapper: A library that simplifies object mapping.

        <!--    https://modelmapper.org/getting-started/    -->
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>${modelmapper.version}</version>
        </dependency>

Create GET endpoint

//User is the parent class of Profile

@Tag(name = "USER", description = "User applicatie Endpoints")
@RequestMapping(RequestPath.V1 + RequestPath.USER)
public interface UserApi {

    @GetMapping("/{code}")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    @Operation(summary = "User weergeven op basis van code.")
    UserDTO getUserByCode(@PathVariable(value = "code")
                          @Parameter(example = "ABCD", description = "test") String code);


}

Create A Pageable GET endpoint to retrieve a list of User’s

@Tag(name = "USER", description = "User applicatie Endpoints")
@RequestMapping(RequestPath.V1 + RequestPath.USER)
public interface UserApi {

    @GetMapping()
    @Operation(summary = "Lijst weergeven met alle users als paging.")
    @Parameter(name = "page", schema = @Schema(type = "integer", defaultValue = "0"), in = ParameterIn.QUERY)
    @Parameter(name = "size", schema = @Schema(type = "integer", defaultValue = "20"), in = ParameterIn.QUERY)
    PagedModel<?> getAllUsersPageable(@ParameterObject @Parameter(hidden = true) Pageable pageable);
}

Create A POST endpoint to create a new user

@Tag(name = "USER", description = "User applicatie Endpoints")
@RequestMapping(RequestPath.V1 + RequestPath.USER)
public interface UserApi {


    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    //@PreAuthorize("hasAuthority('admin:READ)")
    @Operation(summary = "Nieuwe user aanmaken.")
    @ResponseBody
    UserDTO createUser(@Valid @RequestBody UserDTO userDTO);


}

Let’s Stay Connected

Feedback is always welcome. If you have any questions, comments, or suggestions, please do not hesitate to reach out.