Spring Boot - Query Parameters

Aman WalkeAman Walke
3 min read

πŸ‘‹ Introduction

In previous article Getting Started with Spring Boot: Your First "Hello, World!" REST API, we created a simple Spring Boot Rest API which returns a string. Now let’s take things a little further and make our application little dynamic by adding query parameters to our HTTP Request.

πŸ› οΈ Step 1: Create a Spring Boot Project

πŸ”§ Configuration:

  • Project: Maven

  • Language: Java

  • Spring Boot: (Use the latest stable version)

  • Dependencies: Spring Web

  • Package name: com.queryparams or whatever you like.

The configuration should look like this

Click Generate, unzip the downloaded file, and open it in your favourite IDE (IntelliJ IDEA, Eclipse, VS Code). I prefer to use IntelliJ IDEA.

πŸ“ Step 2: Understand the Project Structure

src
└── main
β”œβ”€β”€ java
β”‚ └── com.queryparams
β”‚ β”œβ”€β”€ QueryParamsApplication.java
└── resources
β”œβ”€β”€ application.properties

πŸ‘¨β€πŸ’» Step 3: Create the Controller

package com.amanvw;

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

@RestController
public class QueryParamsController {

    @GetMapping("/hello")
    public String sayHello(@RequestParam String name) {
        return "Hello, " + name + "!";
    }
}
  • @RequestParam is the new annotation we introduced here. This annotation is used to extract query parameters, form parameters or any URL parameters from a HTTP Request and bind them to a controller method parameter.

  • So, in our HTTP request when we provide ?name=something, spring boot will bind it with the sayHello method parameter β€” name.

  • Let’s try it out!

▢️ Step 4: Run the Application

To run the application, you can either

πŸ“¦ Use Maven:

./mvnw spring-boot:run

🧠 Or use your IDE:

Right-click on QueryParamsApplication.java β†’ Run.

You’ll see logs ending with:

Tomcat started on port(s): 8080 (http)

🌐 Step 5: Test the Endpoint

Open your browser or Postman and go to:

http://localhost:8080/hello?name=Aman

βœ… Output:

Hello, Aman!

Now, try not providing the query parameter and see what happens. You’ll see an error page.

🧠 Understand @RequestParam

  • When you provide @RequestParam String name, you implicitly specify that name is required parameter.

  • To make it not-required use, @RequestParam(required = false) String name

  • Also, you can have a different query parameter names in your HTTP Request and the method parameters β€” @RequestParam(required = false, name = "name") String firstName

Let’s try this out by adding following method in our QueryParamsController

    @GetMapping("/hello-address")
    public String sayHelloAddress(@RequestParam String name,
                                  @RequestParam(required = false, name = "address") String place) {
        if (place == null) {
            return "Hello, " + name + "!";
        }
        return "Hello, " + name + " from " + place + "!";
    }

Stop and start the application again.

Open your browser or Postman and go to:

http://localhost:8080/hello-address?name=Aman&address=India

βœ… Output:

Hello, Aman from India!

Since, address is optional:

http://localhost:8080/hello-address?name=Aman

βœ… Output:

Hello, Aman!

πŸ”š Conclusion

You just created a simple Spring Boot application that accepts different query parameters and renders response based on those.

You can find the complete code here: GitHub - AmanVW

Stay tuned for the next part in the series!

Β© Aman Walke

0
Subscribe to my newsletter

Read articles from Aman Walke directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aman Walke
Aman Walke

I'm a tech enthusiast who loves building backend systems that just work β€” clean, scalable, and efficient. I've worked with microservices, Spring Boot, Azure, and APIs, and I enjoy digging into root causes and making systems better. Whether it's writing clean code, reviewing it, or managing deployments with DevOps tools, I'm always up for the challenge. I like working in collaborative environments where I can learn, share, and grow alongside smart people.