Skip to content

Customizable naming support for setter/field for data binding from request headers #34062

@orcunbalcilar

Description

@orcunbalcilar

Hi,

Data binding for request headers works from spring web 6.2.0 on. But setter method naming (so relatively field name) must match with request header name. This could lead to a bug easily if anyone changes the field name.

Could you support annotations on field/setter?

Request

POST http://localhost:8080/api/header
Content-Type: application/json
a: TEST
Content-Length: 198
User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.3.1
Accept-Encoding: br, deflate, gzip, x-gzip
Accept: */*

... (Request Body)

Actual

If we change the name to a different one than a, binding is not done because field name and header name must match.

Class binding is supported.

public class RequestHeadersClass {

          private String a; // if we change the name to a different one than 'a', binding is not done because field name and header name must match.
        
          public void setA(String a) {
              this.a = a;
          }
      
          @Override
          public String toString() {
              return "RequestHeadersClass{" +
                  "a='" + a + '\'' +
                  '}';
          }
      }

But record is not supported yet.

public record RequestHeaders(@RequestHeader String a) {}

Desired

  public class RequestHeadersClass {         

    @ParamName("a")
    private String <anything you want>;
              
    public void setA(String a) {
      this.a = a;
    }
            
    @Override
    public String toString() {
      return "RequestHeadersClass{" +
      "a='" + a + '\'' +
      '}';
    }
  
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions