1

I am trying to add a default value to an optional header parameter for when it is not present. However, when the parameter is not included, I noticed that it is not being populated with the default value:

public Mono<User> getUser(
  @RequestHeader(value = "accept", defaultValue = "abc") String acceptHeader,
  ...
)

When I am debugging the code, acceptHeader is "/" when no the param is not specified. I looked online and could not find any reason as to why this is happening. Not sure if it makes a difference, but I am making the call with Postman. Does anyone have any idea why this is happening? I could implement code to handle this logic inside getUser but I would like to ideally use annotations.

user1927638
  • 1,133
  • 20
  • 42

1 Answers1

0

/ is a default value so is not empty and defaultValue = "abc" doesn't work. If you want to do something when nothing was put into accept value use this instead it defaultValue = "abc":

if (acceptHeader.eqals("/") {
    ... do something 
}
fr3ddie
  • 406
  • 6
  • 17
  • Is there documentation for that? https://www.viralpatel.net/spring-requestheader-example/ According to this, the default value is null if that header param is not provided. – user1927638 Nov 04 '20 at 21:17
  • 1
    Look at this https://www.geeksforgeeks.org/http-headers-accept/ and this https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values . I really don't know why returning value is `*/*` when I use postman without any headers. I wrote what I've observed. – fr3ddie Nov 04 '20 at 21:30
  • According to "https://www.geeksforgeeks.org/http-headers-accept/", because of the fact that this is the Accept header, "If the Accept header is not present in the request, then the server assumes that the client accepts all types of media." "*/* This directive accepts any kind of type/subtype." I was looking to use the @Pattern annotation, so I could just incorporate the "*/*" into the pattern. Alternatively, the solution similar to the one above should work. – user1927638 Nov 05 '20 at 01:28