Skip to main content

Preparations for Creating a RestController

Message to Learners​

Before starting the practical exercise of creating a RestController, it is essential that you take the time to thoroughly read this section. It is not yet time to code, but rather to prepare and understand the theory behind what we are going to do.

1. What is a Web Service?​

A web service is a software component that allows applications to communicate with each other over a network. It exposes functionalities or data that other applications can consume by sending HTTP requests and receiving responses.

Client-Server Architecture​

Communication Flow​


2. Annotations in Spring Boot​

Annotations are metadata added to the code to indicate how certain parts should behave. They allow Spring Boot to automatically configure your application.

Main Annotations​

AnnotationRoleExample
@RestControllerIndicates that the class is a REST controller that handles HTTP requests and returns responses in JSON or XML format@RestController public class CardController
@RequestMappingAssociates a base URL with a class or method@RequestMapping("/cards")
@GetMappingSpecifies that a method responds to HTTP GET requests@GetMapping("/hello")
@PostMappingSpecifies that a method responds to HTTP POST requests@PostMapping("/create")
@PutMappingSpecifies that a method responds to HTTP PUT requests@PutMapping("/update")
@DeleteMappingSpecifies that a method responds to HTTP DELETE requests@DeleteMapping("/delete")

Annotation Hierarchy​


3. Model and Controller​

The Model​

A Model is a Java class that represents a business entity. It contains attributes and methods to manage data.

Example: Card Model

Corresponding Java Code:

public class Card {
private int cardId;
private int customerId;
private String cardNumber;
private String cardType;
private int totalLimit;
private int amountUsed;
private int availableAmount;
private String createDt;

// Getters and Setters
}

The Controller​

A Controller is a class that handles incoming HTTP requests and returns responses. It links the client to the business logic.

Example: CardController

Corresponding Java Code:

@RestController
@RequestMapping("/cards")
public class CardController {

@GetMapping("/hello")
public String helloCard() {
return "Hello, Card!";
}

@GetMapping("/{id}")
public Card getCard(@PathVariable int id) {
// Logic to retrieve a card
}

@PostMapping
public Card createCard(@RequestBody Card card) {
// Logic to create a card
}
}

MVC Architecture with Spring Boot​


4. Technical Prerequisites​

Before starting the practical exercise, ensure that your development environment is properly configured.

Tool Verification​

1. Java Development Kit (JDK)​

Required Version: Java 17 or higher

Verification:

java -version

Expected Result:

java version "17.0.x" or higher

2. Apache Maven​

Required Version: Maven 3.9 or higher (recommended for stability)

Verification:

mvn -version

Expected Result:

Apache Maven 3.9.x or higher
Java version: 17.x.x

Note: Maven 3.9+ offers better stability and compatibility with Java 17+.

3. Visual Studio Code​

Required Extensions:

  • Extension Pack for Java
  • Spring Boot Extension Pack

Development Environment Diagram​


5. Practical Exercise Plan​

Objective​

Learn to create a REST controller to handle HTTP requests by creating a complete Spring Boot application.

Exercise Steps​

Step Details​

Step 1: Project Configuration​

Use Spring Initializr to generate a Spring Boot project with:

  • Project: Maven
  • Language: Java
  • Spring Boot Version: 3.x
  • Dependencies: Spring Web, Spring Boot DevTools

Step 2: Model Creation​

Create a Card class in the model package with the necessary attributes.

Step 3: Controller Creation​

Create a CardController class in the controller package with:

  • @RestController annotation
  • @RequestMapping("/cards") annotation
  • Methods with @GetMapping, @PostMapping, etc.

Step 4: Compilation and Execution with Maven​

Essential Maven Commands:

Option 1: Separate Commands

# Clean the project
mvn clean

# Compile and install (without tests)
mvn install -DskipTests

# Run the application
mvn spring-boot:run

Option 2: Combined Command (RECOMMENDED)

# All in one command
mvn clean install -DskipTests spring-boot:run

Explanation of Commands:

  • mvn clean : Removes previously compiled files
  • mvn install : Compiles the project and creates the JAR file
  • -DskipTests : Skips test execution (faster for development)
  • spring-boot:run : Starts the Spring Boot application

Note: The combined command is more efficient as it executes the entire cycle at once.

Step 6: Testing​

Test the endpoints:

  • http://localhost:8080/cards/hello
  • Use Postman to test other methods

6. Spring Boot Project Structure​


7. HTTP Request Lifecycle​


Key Concepts to Remember​

1. Separation of Concerns​

2. Spring Boot Annotations​

  • @RestController = @Controller + @ResponseBody (automatically returns JSON)
  • @RequestMapping defines the base path
  • @GetMapping/@PostMapping/@PutMapping/@DeleteMapping correspond to HTTP methods

3. Response Format​

By default, Spring Boot automatically converts Java objects to JSON using Jackson.


Conclusion​

This preparatory reading has provided you with:

  • An understanding of web services and how they work
  • Knowledge of essential Spring Boot annotations
  • The distinction between Model and Controller
  • The necessary technical prerequisites
  • An overview of the practical exercise plan

You are now ready to move on to the practical exercise where you will create your first RestController!

Important: If something is unclear, feel free to reread this section or ask questions. A good theoretical understanding will greatly facilitate practice.