SringBoot has this famous construct to extract all request headers:
@RestController
public class AllHeaderController {
@GetMapping("/print-all-headers")
public void getAllheaders(@RequestHeader Map<String,String> headers){
headers.forEach((key,value) ->{
System.out.println("Header Name: "+key+" Header Value: "+value);
});
HeadersInformation headersInformation = headersInformationService.furtherCheckAllHeaders(headers); //just one param here
}
}
Pros: in the event there is a lot of headers, we can get all of them in just one variable.
Cons: This lacks the possibility to have fine-grained control and documentation over each header.
On the other hand, Spring also offers @RequestHeader
per variable:
String oneByOne(@RequestHeader(value = "X-MY-CUSTOM-HEADER-ONE", required = true) final String one,
@RequestHeader(value = "X-MY-CUSTOM-HEADER-TWO", required = false, defaultValue = "two") final String two,
@RequestHeader(value = "X-MY-CUSTOM-HEADER-THREE", required = false) final String three,
@RequestHeader(value = "X-MY-CUSTOM-HEADER-FOUR") @Parameter(description = "some specific swagger description for header four") final String four,
[...] //truncated for convenience, but is it possible to improve here?
@RequestHeader(value = "X-MY-CUSTOM-HEADER-HUNDRED", required = false, defaultValue = "two") @Parameter(description = "some specific description for header 100") final String hundred) {
HeadersInformation headersInformation = headersInformationService.furtherCheckAllHeaders(one, two, thee ... , ninetynine, hundred); //OMG!
Pros: We can annotate the header name, we can add required or not, some default values, and some swagger documentation generation. Also, we get out of the box 400 bad requests if the header is missing, etc
Cons: In the event there are many headers, let’s say in our example 100 method parameters, the method becomes very hard to read and very hard to maintain (add or remove header)
I tried having the best of the two worlds by writing:
String bestOfTwoWorlds(@RequestHeader(value = "X-MY-CUSTOM-HEADER-ONE", required = true),
@RequestHeader(value = "X-MY-CUSTOM-HEADER-TWO", required = false, defaultValue = "two"),
@RequestHeader(value = "X-MY-CUSTOM-HEADER-THREE", required = false),
@RequestHeader(value = "X-MY-CUSTOM-HEADER-FOUR") @Parameter(description = "some specific swagger description for header four"),
@RequestHeader(value = "X-MY-CUSTOM-HEADER-HUNDRED", required = false, defaultValue = "two") @Parameter(description = "some specific description for header 100"),
Map<String, String> String allHeaders) {
HeadersInformation headersInformation = headersInformationService.furtherCheckAllHeaders(headers);
I was hoping something like this would work.
Question:
May I ask if there is a solution where we can have the best of the two worlds, i.e. being able to use a Map of headers in one variable, while also having per header annotation?
2