0% found this document useful (0 votes)
17 views3 pages

@request Param

Uploaded by

Blue Red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

@request Param

Uploaded by

Blue Red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

✅ What is @RequestParam?

@RequestParam is used to extract query parameters (or form data) from the URL and
bind them to method parameters in a Spring controller.
It is used to extract the query parameters form the URL. It is also known as a
query parameter. It is most suitable for web applications. It can specify default
values if the query parameter is not present in the URL.

It's used in both:

@Controller

@RestController

✅ Basic Syntax
java
Copy
Edit
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "Hello " + name;
}
🔹 URL to Call:
bash
Copy
Edit
http://localhost:8080/greet?name=Immi
🔹 Output:
nginx
Copy
Edit
Hello Immi
✅ Full Example with @RestController
java
Copy
Edit
@RestController
public class MyController {

@GetMapping("/welcome")
public String welcome(@RequestParam String user) {
return "Welcome, " + user + "!";
}
}
🧪 URL:
bash
Copy
Edit
http://localhost:8080/welcome?user=Immi
✅ Optional Parameters with Default Values
java
Copy
Edit
@GetMapping("/search")
public String search(@RequestParam(defaultValue = "Java") String keyword) {
return "Searching for: " + keyword;
}
🔹 Calls:
/search?keyword=Spring → "Searching for: Spring"
/search → "Searching for: Java" ✅

✅ Optional Parameter with required = false


java
Copy
Edit
@GetMapping("/profile")
public String profile(@RequestParam(required = false) String name) {
return (name != null) ? "Hello " + name : "Hello Guest";
}
✅ Multiple Parameters
java
Copy
Edit
@GetMapping("/add")
public String add(
@RequestParam int a,
@RequestParam int b
) {
return "Sum is: " + (a + b);
}
🧪 URL:

bash
Copy
Edit
http://localhost:8080/add?a=5&b=10
✅ Output:

csharp
Copy
Edit
Sum is: 15
✅ Comparison with @PathVariable
Feature @RequestParam @PathVariable
From Query parameter URL path
URL /user?id=5 /user/5
Flexible order? ✅ Yes ❌ No
Optional/default? ✅ Yes ❌ No (unless set manually)

package com.example.demo;

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

@RestController
public class GreetingController {

// Example 1: Basic usage of @RequestParam


@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "Hello, " + name + "!";
}

// Example 2: With default value


@GetMapping("/welcome")
public String welcome(@RequestParam(defaultValue = "Guest") String user) {
return "Welcome, " + user + "!";
}

// Example 3: Optional parameter


@GetMapping("/profile")
public String profile(@RequestParam(required = false) String name) {
if (name == null) {
return "Hello, anonymous user!";
}
return "Hello, " + name + "!";
}

// Example 4: Multiple query params


@GetMapping("/add")
public String add(@RequestParam int a, @RequestParam int b) {
return "Sum is: " + (a + b);
}
}

You might also like