In Java, two common methods exist for passing collection parameters via HTTP GET requests: URL parameter concatenation and using the @RequestParam annotation. Each approach serves different scenraios.
URL Paramter Concatenation
This method converts collections into comma-separated strings appended to URLs. Suitable for simple data structures.
// Client-side request construction
List<String> identifiers = List.of("A", "B", "C");
String endpoint = "https://api.example.com/data?keys=" + String.join(",", identifiers);
// Server-side parameter handling
@GetMapping("/fetch")
public String processRequest(@RequestParam List<String> keys) {
// Process keys collection
return "completed";
}
Using @RequestParam for Colllections
The @RequestParam annotation directly binds multiple parameters to collection types. Ideal for handling key-value pairs or complex collections.
// Client-side parameter mapping
Map<String, Object> attributes = Map.of("user", "Bob", "level", 5);
String url = "https://api.example.com/profile?" + buildQueryString(attributes);
// Server-side map processing
@GetMapping("/profile")
public String handleProfile(@RequestParam Map<String, Object> attributes) {
// Utilize attribute map
return "processed";
}
Implementation Note: The
buildQueryStringmethod in the client example would typically URL-encode parameters and format them askey=valuepairs separated by&.