How to specify if a header param is mandatory or optional in micronaut

Viewed 798

I am passing two headers in controller as below

@Header("x-correlationId") String correlationId,
@Header(name = "x-consumedBy") String consumedBy

where x-correlationId is mandatory and x-consumedBy is optional. I am not able to specify this.

In Spring we can specify required=false.

Tell us what happens instead.
It is taking both as mandatory.

If I specify @Nullable then it is always taking the value as null even if I pass the value

correlationId::12345:consumedBy:null
2 Answers

@Nullable is the preferred way to flag an argument as allowing nulls. If that isn't working for you then there must be something else at play that I can't tell from the information provided. It definitely does work, we have many tests that assert that.

By default @Header params are mandatory, but to make x-consumedBy as an optional header you can set defaultValue

fun index(@Header("x-correlationId") xCorrelationId :String,
          @Header("x-consumedBy",defaultValue = "") xConsumedBy :String): String {
    
}
Related