Skip to content

Allow @RequestParam, @PathVariable, and their siblings on fields #23618

@behrangsa

Description

@behrangsa

It is common to use abbreviated names for query parameters in GET requests.

For example:

GET /search?q=java&y=2019&l=eng

Where q means query, y means year, and l means language.

This can then be handled using a controller method:

public SomeResponse search(SearchRequest request) {
...
}

where:

public class SearchRequest {
   private String q;
   private String y;
   private String l;
}

However if we could apply @RequestParam on fields, then we could make SearchRequest more readable:

public class SearchRequest {
   @RequestParam("q")
   private String query;

   @RequestParam("y")
   private String year;

   @RequestParam("l")
   private String language;
}

As shown in the linked SO post, it is possible to customize Spring to do this using a custom annotation (e.g. @ParamName), but I think it would be cleaner and more elegant to allow @RequestParam (and @PathVariable, etc.) on POJO fields.

References

  1. StackOverflow: How to customize parameter names when binding Spring MVC command objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions