Request Param annotation example

Request Param annotation example

The @Request Param annotation in Spring is used to extract query parameters from the URL and bind them to method parameters in a controller. This is useful for handling optional or required parameters that are passed as part of the query string in HTTP GET or POST requests.

Request Param annotation

Example

Step 1: Create a Spring Boot Application

Ensure you have the necessary dependencies in your `pom.xml` (for Maven) or `build.gradle` (for Gradle).

Example
Maven Dependency:
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
```

Gradle Dependency:
```groovy
implementation 'org.springframework.boot:spring-boot-starter-web'
```

Step 2: Create the Main Application Class

Create the main class for the Spring Boot application.

Example
```java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
```

Explanation:

  • @SpringBootApplication: Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.

Example
‘calculateTotalPrice()’, ‘getUserName()’

Step 3: Create a Controller

Create a controller class that uses the @RequestParam annotation to handle incoming requests with query parameters.

Example
```java
package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/greet")
    public String greetUser(@RequestParam(value = "name", defaultValue = "Guest") String name,
                            @RequestParam(value = "age", required = false) Integer age) {
        if (age != null) {
            return "Hello, " + name + "! You are " + age + " years old.";
        } else {
            return "Hello, " + name + "!";
        }
    }
}
``

Explanation:

  • @RestController: Indicates that this class is a REST controller, and the return values of methods are directly written to the HTTP response body.
  • @RequestMapping("/api"): Specifies that all methods in this class handle requests starting with /api.
  • @GetMapping("/greet"): Maps GET requests to /api/greet to the greetUser method.
  • @RequestParam(value = "name", defaultValue = "Guest"): Binds the name query parameter to the name method parameter. If name is not provided, it defaults to "Guest".
  • @RequestParam(value = "age", required = false): Binds the age query parameter to the age method parameter. If age is not provided, it is set to null.

Step 4: Running the Application

Run the application from the main class (DemoApplication). You can test the endpoint using a tool like Postman or by navigating to the URLs in your browser:

Example

- GET request to `http://localhost:8080/api/greet?name=John&age=30` should return `"Hello, John! You are 30 years old."`.
- GET request to `http://localhost:8080/api/greet?name=Jane` should return `"Hello, Jane!"`.
- GET request to `http://localhost:8080/api/greet` should return `"Hello, Guest!"`.

Conclusion Request Param annotation example

  • @RequestParam: Used to extract query parameters from the URL and bind them to method parameters. It supports default values and optional parameters.
  • Example: Demonstrated how to use @RequestParam to handle query parameters and provide default values when parameters are not present.

Homepage

Readmore