Category: Spring Updated: Jul 31, 2026

Spring Boot GetMapping Tutorial: Complete Guide to GetMapping in Java for Beginners

Spring @GetMapping Annotation Tutorial for Beginners
Spring @GetMapping Annotation Tutorial for Beginners

Let us learn through a conversation between a teacher and a student

Student

"How do I handle HTTP GET requests in a Spring Boot web application using annotations?"

Teacher

"You can handle HTTP GET requests easily by using the @GetMapping annotation on your controller methods to map specific URL paths."

Student

"Can we see how to use GetMapping with path variables and parameters step by step?"

Teacher

"Let us explore the complete guide to using @GetMapping in Spring for Java beginners!"

1. Introduction to GetMapping and Spring MVC Web Basics

Key Takeaway: Let's dive into the core concepts of "1. Introduction to GetMapping and Spring MVC Web Basics" cleanly and effectively.

1. Introduction to GetMapping and Spring MVC Web Basics
1. Introduction to GetMapping and Spring MVC Web Basics

Welcome to the complete Java tutorial for beginners on mastering the @GetMapping annotation in Spring Boot. When developing modern web applications and RESTful APIs using Java programming basics and Spring MVC, handling incoming GET requests is the primary way clients retrieve resources from a server. Understanding how routing works allows you to build responsive and interactive web applications efficiently.

In this comprehensive guide, you will learn how to map URL endpoints, extract query parameters, capture dynamic path variables, and return proper payloads using @GetMapping. We will use clean Java code and proper structural organization to ensure an intuitive and seamless learning experience for every Java beginner.

Spring Boot streamlines request routing by replacing verbose configuration declarations with intuitive method-level annotations. By mastering these core capabilities, you will understand how specific web addresses link directly to executable Java functions.

2. Basic Usage of GetMapping for Simple Endpoints

2. Basic Usage of GetMapping for Simple Endpoints
2. Basic Usage of GetMapping for Simple Endpoints

The standard way to use @GetMapping is to attach it to a controller method and pass the target URL path value inside parentheses. When a client browser sends an HTTP GET request matching that path, Spring executes the designated method and returns the result.

Below is a simple Java class demonstrating how to define a basic GET endpoint in a Spring Boot controller.


@RestController
public class WelcomeController {

    @GetMapping("/welcome")
    public String sayWelcome() {
        return "Welcome to Spring Boot GetMapping tutorial!";
    }
}

The @RestController annotation ensures that the string returned by sayWelcome is sent directly as the HTTP response payload.

3. Combining GetMapping with Request Parameters

3. Combining GetMapping with Request Parameters
3. Combining GetMapping with Request Parameters

Often, GET requests require supplementary query values passed via the URL, such as search terms or filters (e.g., /search?query=java). You can combine @GetMapping with the @RequestParam annotation inside your method signature to extract these inputs.

Study the following Java code snippet to see how a controller handles optional or mandatory query parameters on a GET route.


@RestController
public class SearchQueryController {

    @GetMapping("/search")
    public String searchContent(
            @RequestParam(value = "query", defaultValue = "Spring") String query) {
        return "Search results for query: " + query;
    }
}

This implementation ensures safe fallback execution when a client hits the search path without appending explicit query string parameters.

4. Capturing Dynamic Path Variables with GetMapping

Key Takeaway: Let's dive into the core concepts of "4. Capturing Dynamic Path Variables with GetMapping" cleanly and effectively.

4. Capturing Dynamic Path Variables with GetMapping
4. Capturing Dynamic Path Variables with GetMapping

When you need to retrieve a specific resource identified by an ID or name, embedding the identifier directly in the URL path is standard practice (e.g., /products/42). You can specify placeholders inside braces in @GetMapping and capture them via @PathVariable.

The code example below illustrates how to capture dynamic path segments cleanly in a GET request handler.


@RestController
public class ProductController {

    @GetMapping("/products/{id}")
    public String getProductDetail(@PathVariable("id") Long productId) {
        return "Fetching details for product identifier: " + productId;
    }
}

This design pattern yields clean, readable URLs that integrate seamlessly with modern REST architectural guidelines.

5. Returning JSON Objects and Complex Data Structures

5. Returning JSON Objects and Complex Data Structures
5. Returning JSON Objects and Complex Data Structures

Instead of returning raw text strings, real-world REST applications use @GetMapping to return structured Java objects that Spring automatically serializes into JSON payloads for frontend clients or mobile applications.

Review the following controller configuration showing how a GET mapping returns a custom data record or object representation.


@RestController
public class DataController {

    public static class SystemInfo {
        public String name = "Spring Boot API";
        public String version = "3.0";
    }

    @GetMapping("/api/info")
    public SystemInfo getSystemInfo() {
        return new SystemInfo();
    }
}

Jackson library integrated within Spring Boot handles object-to-JSON conversion automatically behind the scenes.

6. Grouping Routes with Class-Level RequestMapping

6. Grouping Routes with Class-Level RequestMapping
6. Grouping Routes with Class-Level RequestMapping

If your controller contains multiple GET endpoints that share a common prefix root path, you can annotate the entire controller class with @RequestMapping to keep your code organized and DRY (Don't Repeat Yourself).

Here is an example showing multiple @GetMapping definitions unified under a shared class-level prefix mapping.


@RestController
@RequestMapping("/courses")
public class CourseController {

    @GetMapping("/list")
    public String listCourses() {
        return "Listing all available Java courses.";
    }

    @GetMapping("/featured")
    public String featuredCourse() {
        return "Featured course: Spring Boot Basics.";
    }
}

This structure organizes related API routing blocks into logical modules, making large projects easier to scale and maintain.

7. Troubleshooting GetMapping Routing and Mapping Conflicts

Key Takeaway: Let's dive into the core concepts of "7. Troubleshooting GetMapping Routing and Mapping Conflicts" cleanly and effectively.

7. Troubleshooting GetMapping Routing and Mapping Conflicts
7. Troubleshooting GetMapping Routing and Mapping Conflicts

Beginners occasionally encounter routing conflicts, such as duplicate mapping exceptions or 404 Not Found responses when configuring @GetMapping. A duplicate mapping error occurs if two different methods in your application try to map the exact same HTTP GET path and parameters.

Carefully verifying unique path strings, ensuring correct package component scanning, and validating parameter configurations will resolve routing irregularities. Systematic testing ensures predictable execution throughout application lifecycles.

Proper adherence to endpoint specifications guarantees robust structural compliance and stable operation of your web services.

Summary

Summary
Summary

In this comprehensive guide, we examined how to utilize the @GetMapping annotation to handle HTTP GET requests efficiently in Spring Boot applications and RESTful APIs. We covered basic URL routing, extracting query values with @RequestParam, capturing dynamic path identifiers via @PathVariable, returning serialized JSON objects, and organizing multi-endpoint controllers using class-level request mapping. Mastering these fundamentals empowers Java beginners to build robust and scalable web services using clean and simplified Spring annotations.


@RestController
public class SpringSummaryController {
    @GetMapping("/api/summary")
    public String getSummaryMessage() {
        return "Spring Boot GetMapping mastery achieved!";
    }
}
Review Conversation Between the Teacher and Student

Student

"Now I see how clean annotations like @GetMapping connect HTTP GET requests straight to our Spring controller methods for returning text or JSON data without long prefixes!"

Teacher

"Exactly. Importing the short annotation names makes your code much more readable and concise."

Student

"And using path variables makes pulling dynamic resource IDs very intuitive."

Teacher

"Spot on! Keep experimenting with different routing patterns, and you will build professional Spring web APIs with confidence."

Back to Category
Latest Articles
New1
Spring
Spring Boot and Java Version Compatibility Guide: Spring Boot 3.5 3.4 3.3 with Java 21 and Java 17
New
New2
Spring
Complete Guide to Spring Data JPA getReferenceById for Beginners
New
New3
Spring
Complete Guide to Spring Data JPA findAll for Beginners
New
New4
Spring
Spring Data JPA JpaRepository Tutorial for Beginners: Complete Guide to Database Access in Spring Boot
New
Popular Articles
No.1
Java&Spring記事人気No1
Spring
Complete Guide to Spring @GeneratedValue for Java Beginners – Understand Primary Key Generation in JPA
No.2
Java&Spring記事人気No2
Spring
Complete Guide to Spring @PreAuthorize for Java Beginners – Secure Your Application with Method-Level Security
No.3
Java&Spring記事人気No3
Spring
Spring Boot GetMapping Tutorial: Complete Guide to GetMapping in Java for Beginners
No.4
Java&Spring記事人気No4
Spring
Spring @Valid Annotation Tutorial for Beginners: Input Validation in Spring Boot
No.5
Java&Spring記事人気No5
Spring
Mastering the @PostMapping Annotation in Java Spring: A Complete Guide for Beginners
No.6
Java&Spring記事人気No6
Spring
Spring Data JPA Like Query Guide: findByFirstnameLike for Java Beginners
No.7
Java&Spring記事人気No7
Spring
Mastering the @Id Annotation in Java Spring: A Complete Guide for Beginners
No.8
Java&Spring記事人気No8
Spring
Mastering the @Entity Annotation in Java Spring: A Complete Guide for Beginners