Table of contents
1.
Introduction
2.
What is meant by @GetMapping annotation?
2.1.
Key Features of @GetMapping
3.
Examples of @GetMapping annotation
3.1.
1. Basic Example of @GetMapping
3.2.
2. @GetMapping with Path Variable
3.3.
3. @GetMapping with Request Parameters
3.4.
4. @GetMapping Returning JSON Response
3.5.
5. @GetMapping with Custom Response Type
4.
Frequently Asked Questions
4.1.
What is the purpose of @GetMapping in Spring Boot?
4.2.
How is @GetMapping different from @RequestMapping?
4.3.
Can @GetMapping return JSON responses?
5.
Conclusion
Last Updated: Feb 15, 2025
Medium

@GetMapping Annotation in Spring Boot

Author Gaurav Gandhi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Spring Boot, the @GetMapping annotation is used to handle HTTP GET requests in RESTful web services. It simplifies mapping URLs to specific controller methods, making it easier to retrieve data from the server. This annotation is commonly used for fetching resources like user details or product lists.

@GetMapping Annotation in Spring Boot

In this article, you will learn the syntax, usage, and examples of @GetMapping in Spring Boot to build efficient REST APIs.

What is meant by @GetMapping annotation?

@GetMapping is an annotation in Spring Bootthat is used to map HTTP GET requests to handler methods in a REST controller. It simplifies the process of defining GET request endpoints, making the code more readable and maintainable.

Key Features of @GetMapping

  1. Simplifies Request Mapping – It is a shortcut for @RequestMapping(method = RequestMethod.GET).
     
  2. Supports Path Variables – It allows handling dynamic URL paths.
     
  3. Can Accept Request Parameters – Enables passing parameters in the URL.
     
  4. Supports Producing Different Content Types – Helps specify the response format (JSON, XML, etc.).
     
  5. Used in REST APIs – Commonly used in developing RESTful services.

Examples of @GetMapping annotation

1. Basic Example of @GetMapping

The simplest use case of @GetMapping is returning a string response when a user accesses a specific endpoint.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Welcome to Spring Boot!";
    }
}

 

Explanation:

  • @RestController marks this class as a REST controller.
     
  • @RequestMapping("/api") sets a base path for all endpoints in this controller.
     
  • @GetMapping("/hello") maps the /hello endpoint to the sayHello method.
     
  • When accessed via http://localhost:8080/api/hello, it returns Hello, Welcome to Spring Boot!.

2. @GetMapping with Path Variable

Sometimes, we need to pass values through the URL and use them in our application. This can be done using Path Variables.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {
    
    @GetMapping("/user/{name}")
    public String greetUser(@PathVariable String name) {
        return "Hello, " + name + "! Welcome to Spring Boot.";
    }
}

 

Explanation:

  • @PathVariable extracts the value from the URL and passes it as a method parameter.
     
  • If we access http://localhost:8080/api/user/Alex, it returns Hello, Alex! Welcome to Spring Boot.

3. @GetMapping with Request Parameters

Sometimes, we want to accept query parameters instead of path variables. We use @RequestParam for this.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {
    
    @GetMapping("/welcome")
    public String welcomeUser(@RequestParam String name, @RequestParam int age) {
        return "Welcome " + name + "! You are " + age + " years old.";
    }
}

 

Explanation:

  • @RequestParam extracts values from the URL’s query parameters.
     
  • If we access http://localhost:8080/api/welcome?name=John&age=25, it returns Welcome John! You are 25 years old.

4. @GetMapping Returning JSON Response

A common use case of @GetMapping is returning JSON responses instead of plain text.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Arrays;

@RestController
@RequestMapping("/api")
public class MyController {
    
    @GetMapping("/students")
    public List<String> getStudents() {
        return Arrays.asList("Alice", "Bob", "Charlie");
    }
}

 

Explanation:

  • The method returns a list of student names as JSON.
     
  • When accessed at http://localhost:8080/api/students, the output is:
["Alice", "Bob", "Charlie"]

5. @GetMapping with Custom Response Type

Sometimes, we need to return a complex object as a response. This is possible by creating a Java class and returning it as JSON.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }
    public int getAge() { return age; }
}

@RestController
@RequestMapping("/api")
public class MyController {
    
    @GetMapping("/student")
    public Student getStudent() {
        return new Student("David", 22);
    }
}

 

Explanation:

  • We create a Student class with name and age attributes.
     
  • The method getStudent() returns a new Student object.
     
  • When accessed at http://localhost:8080/api/student, the output is:
{
  "name": "David",
  "age": 22
}

 

Frequently Asked Questions

What is the purpose of @GetMapping in Spring Boot?

@GetMapping is used to map HTTP GET requests to specific handler methods inside a controller. It simplifies handling GET requests in a REST API.

How is @GetMapping different from @RequestMapping?

@GetMapping is a specialized version of @RequestMapping(method = RequestMethod.GET), making the code more readable and concise.

Can @GetMapping return JSON responses?

Yes, @GetMapping can return JSON responses when the method returns an object or list. Spring Boot automatically converts the response into JSON format.

Conclusion

In this article, we learned the @GetMapping annotation in Spring Boot, which is used to handle HTTP GET requests in RESTful web services. It simplifies request mapping by making the code more readable and efficient. Understanding @GetMapping helps developers build well-structured APIs that retrieve data easily, improving the performance and maintainability of web applications.

Live masterclass