Skip to content
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

Support data binding from request headers #32676

Open
kalgon opened this issue Apr 19, 2024 · 2 comments
Open

Support data binding from request headers #32676

kalgon opened this issue Apr 19, 2024 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@kalgon
Copy link

kalgon commented Apr 19, 2024

Affects: 6.1.5

A rest controller with the following method

    @GetMapping("/{path}")
    public void get(Params params, @PathVariable long path, @RequestParam String foo, @RequestHeader String accept) {
        System.out.printf("path: %d%n", path);
        System.out.printf("foo: %s%n", foo);
        System.out.printf("accept: %s%n", accept);
        System.out.printf("params: %s%n", params);
    }
    public record Params(@PathVariable long path, @RequestParam String foo, @RequestHeader String accept) {}

will produce the following log when called with /1?foo=FOO:

    path: 1
    foo: FOO
    accept: application/json, text/plain, */*
    params: Params[path=1, foo=FOO, accept=null]

@PathVariable and @RequestParam are correctly used to instantiate the record but the @RequestHeader remains null (although it works as a parameter of the controller method)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 19, 2024
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 19, 2024
@sbrannen
Copy link
Member

Hi @kalgon,

Thanks for creating this issue.

@PathVariable and @RequestParam are correctly used to instantiate the record but the @RequestHeader remains null (although it works as a parameter of the controller method)

I have confirmed that to be the case.

@rstoyanchev, is it intentional that @RequestHeader is not supported for this use case?

@snicoll snicoll added the status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team label Apr 19, 2024
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: waiting-for-internal-feedback An issue that needs input from a member or another Spring Team labels Apr 23, 2024
@jhoeller jhoeller added this to the 6.2.x milestone Apr 23, 2024
@rstoyanchev rstoyanchev self-assigned this Apr 24, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Apr 24, 2024

This is regular data binding at work, but applied through constructor args. Note that the @PathVariable, @RequestParam, and @RequestHeader annotations on the record fields play no role at all, so you can drop them unless you want to keep them for clarity.

For data binding we've long supported using both request parameters and path variables. We can add headers to the map of values to bind from.

@rstoyanchev rstoyanchev changed the title Allow @RequestHeader to be injected in constructors like @PathVariable and @RequestParam Support data binding from request headers Apr 24, 2024
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants