Explain Codes LogoExplain Codes Logo

Binding a list in @RequestParam

java
parameter-binding
request-param
java-annotations
Alex KataevbyAlex Kataev·Dec 29, 2024
TLDR

Incorporate multiple values in a list via @RequestParam as follows:

@GetMapping("/items") public String getItems(@RequestParam List<String> ids) { // Byid special items ;] return "Processed ids: '" + ids + "'"; }

Invoke via /items?ids=id1&ids=id2. This maps ids query parameters directly to a List<String>.

Additional defaults and array alignments

You may require default values for scenarios where no parameters are forwarded, or bind the parameters to an array instead of a list. Take an insight:

@GetMapping("/demo") public String demoItems( @RequestParam(defaultValue = "default") String title, @RequestParam(value = "tags[]", required = false) String[] tags) { // Get down to logic. If no tags? Simple, disappear like Mad Hatter's sanity. return "Demo title: '" + title + "'. Tags: '" + Arrays.toString(tags) + "'"; }

With no tags[] inputted, the tags array remains empty. The defaultValue attribute in @RequestParam allows for parameter fallback in its absence.

Parameter parties and collection hullabaloos

The Wrapper class escapade

Cover your list in a class for heavier lifting or sidestepping confusion:

class Demo { private List<String> tags; // getters and setters. Boring but handy, like a good screwdriver. } @GetMapping("/demo") public String demoResult(@ModelAttribute Demo demo) { // Use demo.getTags(). Swoosh! return "Filtered by tags: " + demo.getTags(); }

This technique allows automatic binding when the request names follow the Demo class field names, scrapping the need for additional annotations. Neat, eh?

The Name of Parameter game

Ensure matching query parameters and list's variable names for successful stories:

@GetMapping("/articles") public String getArticles(@RequestParam List<String> tags) { // Engaging with tags return "Articles with tags: " + tags; }

Throw in that request like ?tags=java&tags=spring and voila! The tags are at your service.

HTTP protocol rollercoasters

Use path parameters or query strings with GET requests:

@GetMapping("/books") public String getBooks(@RequestParam List<String> authors) { return "Books by authors: " + authors; }

Invoke with /books?authors=Rowling,King. For POST requests, the Listhides stealthily inside the body.

Combat the commonplace problems

Missing Parameter binding

On unbinding parameters, scrutinize the request methods and parameter names. For form data, use @PostMapping, whereas for query parameters, use @GetMapping.

Special character mannerisms

URLs can act up with some characters. To tackle such tantrums, URL encode the fussy characters in the query parameters.

Nesting object handling

For infolded objects, a dotted notation puts all at ease:

class Author { private Book book; // getters and setters } class Book { private List<String> titles; // getters and setters } @GetMapping("/author") public String getAuthorBooks(@ModelAttribute Author author) { // Reaching the very bowels of nested titles list return "Books: " + author.getBook().getTitles(); }

Query the nested list like ?book.titles=book1&book.titles=book2. 🌳