Category: Spring Updated: Jul 20, 2026

Spring Boot RequestParam Tutorial: How to Receive Parameters in Java for Beginners

Spring @RequestParam Annotation Tutorial for Beginners
Spring @RequestParam Annotation Tutorial for Beginners

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

Student

"How can I receive query parameters sent from a URL in a Spring Boot web application?"

Teacher

"In Spring Boot, you can easily capture URL query parameters using the @RequestParam annotation inside your controller methods."

Student

"Can we see how to retrieve single and multiple parameters step by step?"

Teacher

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

1. Introduction to RequestParam and Spring MVC Basics

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

1. Introduction to RequestParam and Spring MVC Basics
1. Introduction to RequestParam and Spring MVC Basics

Welcome to the complete Java tutorial for beginners on handling query parameters with Spring Boot. When building dynamic web applications using Java programming basics and Spring MVC, capturing data sent from a client URL is an essential skill. Understanding HTTP requests and controller parameters will allow you to build interactive websites effectively.

In this comprehensive guide, you will learn how to extract values passed in a URL query string, handle default values, and process optional parameters using the @RequestParam annotation. We will use clean Java code and proper HTML structure to ensure an intuitive learning experience for every Java beginner.

Spring Boot simplifies web development by mapping incoming request parameters directly to method arguments. By mastering this mechanism, your server can respond dynamically based on the input provided by the user in the address bar or through links.

2. Basic Usage of RequestParam for Single Values

2. Basic Usage of RequestParam for Single Values
2. Basic Usage of RequestParam for Single Values

The simplest way to use @RequestParam is to bind a single query parameter from the URL to a method parameter in your Spring controller. For instance, if a user visits /greet?name=Alice, the controller can capture the value of name and use it in the response logic.

Below is a simple Java controller class demonstrating how to receive a basic string parameter using Spring annotations.


@org.springframework.stereotype.Controller
public class GreetingController {

    @org.springframework.web.bind.annotation.GetMapping("/greet")
    @org.springframework.web.bind.annotation.ResponseBody
    public String greetUser(@org.springframework.web.bind.annotation.RequestParam("name") String name) {
        return "Hello, " + name + "! Welcome to Spring Boot.";
    }
}

If the parameter name in the URL matches your method argument name, you can omit the explicit value inside the annotation for brevity.

3. Handling Multiple Query Parameters in a Controller

3. Handling Multiple Query Parameters in a Controller
3. Handling Multiple Query Parameters in a Controller

Real-world applications frequently require multiple inputs at the same time, such as a first name and an age, or search keywords and pagination numbers. You can declare multiple @RequestParam annotations separated by commas in your method signature.

Study the following Java code snippet to see how a controller handles multiple parameters simultaneously from a single request URL.


@org.springframework.stereotype.Controller
public class ProfileController {

    @org.springframework.web.bind.annotation.GetMapping("/profile")
    @org.springframework.web.bind.annotation.ResponseBody
    public String showProfile(
            @org.springframework.web.bind.annotation.RequestParam("name") String name,
            @org.springframework.web.bind.annotation.RequestParam("age") int age) {
        return "User Profile: Name = " + name + ", Age = " + age;
    }
}

Spring automatically converts string values from the URL into numeric types like int or long when specified in the method signature, making data handling seamless.

4. Setting Default Values with Required and DefaultValue

Key Takeaway: Let's dive into the core concepts of "4. Setting Default Values with Required and DefaultValue" cleanly and effectively.

4. Setting Default Values with Required and DefaultValue
4. Setting Default Values with Required and DefaultValue

By default, parameters marked with @RequestParam are mandatory. If a client omits a required parameter, Spring throws a 400 Bad Request error. To prevent this, you can specify whether a parameter is optional or provide a fallback value using the defaultValue attribute.

The code example below shows how to handle optional search queries with a default value when the parameter is missing from the URL.


@org.springframework.stereotype.Controller
public class SearchController {

    @org.springframework.web.bind.annotation.GetMapping("/search")
    @org.springframework.web.bind.annotation.ResponseBody
    public String searchItems(
            @org.springframework.web.bind.annotation.RequestParam(value = "keyword", defaultValue = "Java") String keyword) {
        return "Searching for results matching: " + keyword;
    }
}

This approach protects your application from crashing when users navigate to a root endpoint without appending expected query strings.

5. Receiving Multiple Values as a List or Array

5. Receiving Multiple Values as a List or Array
5. Receiving Multiple Values as a List or Array

Sometimes a single parameter key can have multiple values in a URL, such as filtering items by multiple categories like /filter?category=java&category=spring. Spring allows you to capture these recurring parameters directly into a Java List or array.

Review the following controller method configuration to see how to collect list elements from multiple query parameters.


@org.springframework.stereotype.Controller
public class FilterController {

    @org.springframework.web.bind.annotation.GetMapping("/filter")
    @org.springframework.web.bind.annotation.ResponseBody
    public String filterCategories(
            @org.springframework.web.bind.annotation.RequestParam("category") java.util.List<String> categories) {
        return "Selected categories: " + categories.toString();
    }
}

Using collections enables robust batch processing of multi-select filter controls straight from client requests.

6. Displaying Parameter Results in View Templates

6. Displaying Parameter Results in View Templates
6. Displaying Parameter Results in View Templates

While returning raw string responses using @ResponseBody is helpful for testing, production applications typically forward captured request parameters to an HTML view rendered via Thymeleaf templates.

Here is an example of a controller method that puts parameter data into a Spring Model object for rendering on an HTML page.


@org.springframework.stereotype.Controller
public class PageController {

    @org.springframework.web.bind.annotation.GetMapping("/display")
    public String displayData(
            @org.springframework.web.bind.annotation.RequestParam("title") String title,
            org.springframework.ui.Model model) {
        model.addAttribute("pageTitle", title);
        return "displayPageView";
    }
}

By passing data to the model, you can display dynamic information seamlessly using standard Thymeleaf attributes in your HTML layout files.

7. Troubleshooting Common Parameter Exceptions

Key Takeaway: Let's dive into the core concepts of "7. Troubleshooting Common Parameter Exceptions" cleanly and effectively.

7. Troubleshooting Common Parameter Exceptions
7. Troubleshooting Common Parameter Exceptions

Beginners often encounter errors such as missing parameter exceptions or automatic conversion failures when working with @RequestParam. If an integer argument is expected but a non-numeric string is passed, or if a mandatory parameter is left out without a default value, Spring handles the exception via status codes.

Checking your request URLs for exact spelling matches with your annotation strings resolves most parameter mapping bugs quickly. Testing endpoints with default configurations ensures robust performance across varied user interactions.

Remember that careful parameter validation safeguards your backend code integrity and elevates overall application reliability for web users.

Summary

Summary
Summary

In this comprehensive tutorial, we explored how to handle and receive query parameters in Spring Boot applications using the powerful @RequestParam annotation. Starting with the basics of HTTP request mapping and URL query strings, we covered single parameter injection, multiple parameter combinations, fallback mechanisms with default values, and processing multi-value collections like lists and arrays. Additionally, we demonstrated how controller endpoints pass extracted parameter data into Spring Model objects for seamless rendering in client views. By understanding these core concepts and following standard debugging practices, you can effectively build interactive, robust web applications using Java programming basics and Spring MVC.


public class SummaryHelper {
    public static void logSummary() {
        System.out.println("RequestParam handling is fully mastered!");
    }
}
Review Conversation Between the Teacher and Student

Student

"Now I understand how @RequestParam maps URL query values directly into controller method arguments without extra parsing code!"

Teacher

"Exactly right! It streamlines data retrieval and allows you to build dynamic responses with minimal effort."

Student

"And using defaultValue is a lifesaver to prevent bad requests when users omit optional parameters."

Teacher

"Spot on. Keep practicing these techniques, and you will be developing professional Spring Boot web apps in no time!"

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 @Repository 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