简体   繁体   English

Spring @RequestHeader defaultValue 用于不需要的标头参数

[英]Spring @RequestHeader defaultValue for header parameter that is not required

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.当我调试代码时,如果没有指定参数, acceptHeader是“ / ”。 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.不确定它是否有所作为,但我正在与 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.我可以在getUser实现代码来处理这个逻辑,但我想理想地使用注释。

/ is a default value so is not empty and defaultValue = "abc" doesn't work. /是默认值,因此不为空,并且defaultValue = "abc"不起作用。 If you want to do something when nothing was put into accept value use this instead it defaultValue = "abc" :如果你想在没有任何东西被放入accept值时做某事,请使用它来代替它defaultValue = "abc"

if (acceptHeader.eqals("/") {
    ... do something 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM