Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
behrangsa opened this issue Sep 11, 2019 · 8 comments
Closed

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

behrangsa opened this issue Sep 11, 2019 · 8 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@behrangsa
Copy link

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.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 11, 2019
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 11, 2019
@nictas
Copy link

nictas commented Sep 18, 2019

This issue is a possible duplicate of #23094 and #21770, but I would really like to see it implemented. Not only will it solve the issue with shortened parameter names, but it would also allow us to construct a POJO from headers/parameters with names that are not legal Java identifiers (for example "x-cf-applicationid").

@behrangsa
Copy link
Author

Also related: #22047.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 9, 2019
@rstoyanchev rstoyanchev added this to the 5.x Backlog milestone Dec 9, 2019
@rstoyanchev
Copy link
Contributor

This should be considered together with #22047 indeed, and there are also #23094 and #21770 that can be considered superseded.

@stdevi
Copy link

stdevi commented Apr 27, 2021

Hi, could you provide any recommendation on how to start proceeding with this feature?

@Johnny850807
Copy link

Hi, I'm new here.

I'm wondering if is it a good idea to introduce a new annotation like @ParamName and use it with @RequestParam to address the scenario mentioned in the original post. Here's an example:

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

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

   @ParamName("q")
   private String year;

   @ParamName("q")
   private String language;
}

@jhoeller
Copy link
Contributor

jhoeller commented Jan 3, 2024

We have no plans to introduce field-level request binding annotations. @RequestParam, @PathVariable and co are meant to be used at the parameter level, that's a pretty central part of our annotated handler method arrangement. Binding to objects is a separate mechanism based on bean property setters, property-style fields or constructor arguments, matched against request parameters and path variables. If different binding names are intended there, you could use constructor binding and declare the constructor parameter names with the abbreviated binding names, or with setter methods and declare the setter method names according to your binding names, all while keeping the field names themselves readable.

@jhoeller jhoeller closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2024
@jhoeller jhoeller added the status: declined A suggestion or change that we don't feel we should currently apply label Jan 3, 2024
@jhoeller jhoeller removed this from the 6.x Backlog milestone Jan 3, 2024
@vcruzmj

This comment was marked as duplicate.

@mammadyahyayev
Copy link

It would be great if this is supported by default, but you can achieve the same thing with custom method argument resolvers. You can read my solution from here: https://stackoverflow.com/a/79132213/13452926

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

10 participants