java / beginner
Snippet
Using Query Parameters
@RequestParam is used to read query parameters from the URL (e.g., /search?q=java). You can define default values to make the parameter optional.
snippet.java
1
2
3
4
@GetMapping("/search")public String search(@RequestParam(name = "q", defaultValue = "none") String query) {return "Result for: " + query;}
spring
Breakdown
1
@RequestParam(name = "q")
Extracts the parameter named 'q' from the URL query string.
2
defaultValue = "none"
Sets a fallback value if the parameter is not provided.