Step-By-Step Implementation of @RequestMapping annotation

For this article, we’ll be using the following tools:

  • Java 8 or higher
  • Java IDE like Eclipse, IntelliJ, and VS code (We’ll be using IntelliJ)
  • POSTMAN for testing Request Mappings
  • Dependency: Spring Web Starter

Step-1: Create a starter file and extract it

Go to Spring Initializr and create a starter file having a single dependency – Spring Web. Download and extract it into your local folder and open it in your favorite IDE.

pom.xml File:

XML




<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.w3wiki</groupId>
    <artifactId>RequestMappingExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RequestMappingExample</name>
    <description>Request Mapping Example</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
            </plugin>
        </plugins>
    </build>
  
</project>


Step-2: Define a Controller

  • Create a new package for containing all the controllers that we will be adding in our application under the src/main/java/package_name/Controllers
  • Add a new TestController inside Controllers Package for defining Request Mappings. Your final directory structure would look something like this :

Step-4: Define URL Templates inside the controller

Annotate the Controller with @Controller to signify that this class is a controller that has some URL templates defined inside it and @RequestMapping on the controller as well as the methods to specify which URL path will give what output.

Below is the code Implementation of Controller:

Java




import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
  
import java.util.ArrayList;
import java.util.List;
  
// Controller class
@Controller
@ResponseBody
@RequestMapping("/test")
public class TestController {
  
    // URL Path - 1
    // Returns a String
    @RequestMapping("/hello")
    public String sayHello() {
        System.out.println("dsvsdvdsvs");
        return "Hello Geek!";
    }
  
    // URL Path - 2
    // Returns a String
    @RequestMapping("/sport")
    public String doSomeSport() {
        return "Run 5 kilometers today!";
    }
  
    // URL Path - 3
    // Returns a List
    @RequestMapping("/today/tasks")
    public List<String> todaysTasks() {
  
        List<String> myTasks = new ArrayList<String>();
        myTasks.add("Write 5 articles on w3wiki Today");
        myTasks.add("Run 5 kilometers");
        myTasks.add("Do Laundry");
  
        return myTasks;
    }
}


Explanation of the above Program:

We’ve defined multiple end points using @RequestMapping which can be accessed at following URLs :

  • http://localhost:8080/test/hello
  • http://localhost:8080/test/sport
  • http://localhost:8080/test/today/tasks

Spring Boot – @Requestmapping

Spring Boot is the most popular framework of Java for building enterprise-level web applications and back-ends. Spring Boot has a handful of features that support quicker and more efficient web app development. Some of them are Auto-configuration, Embedded Server, opinionated defaults, and Annotation Support. In this article, we’ll be exploring the core annotation of Spring Boot – @RequestMapping which is part of the set of annotations that Spring Boot employs for defining URL endpoints and REST APIs.

Similar Reads

@RequestMapping

This annotation is a versatile and flexible annotation that can be used with a controller (class) as well as the methods to map specific web requests with the handler methods and controllers. This annotation is part of a larger set of annotations provided by Spring Framework to define URL endpoints and simplify the development of Spring Boot applications....

Step-By-Step Implementation of @RequestMapping annotation

For this article, we’ll be using the following tools:...

Annotations used:

...

Conclusion

...

Contact Us