在生产环境中出现MissingRequestHeaderException,但在本地环境中没有出现。

huangapple go评论50阅读模式
英文:

MissingRequestHeaderException on production but not on local

问题

在生产环境中调用GET控制器时,出现以下异常:

Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'user_id' for method parameter type String is not present]

如果我尝试使用Postman,则会得到400 Bad Request,在本地运行正常。

这是我的控制器:

    @GetMapping(path = "/get")
    public ResponseEntity<UserDto> getUserInformationById(@RequestHeader("user_id") final String id){
        UserDto userDto = userInformationService.getUserInformationById(id);
        if (userDto==null)
            return ResponseEntity.notFound().build();
        return ResponseEntity.ok(userDto);
    }

为什么在生产环境中失败,而在本地环境中没有问题?相同类的其他类似端点正常工作,我漏掉了什么?

英文:

I'm getting the next exeption when I call GET controller in production, in local works fine:

Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header &#39;user_id&#39; for method parameter type String is not present]

If I try using postman I got 400 Bad Request, in local works fine.

Here is my controler

    @GetMapping(path =&quot;/get&quot;)
    public ResponseEntity &lt;UserDto&gt; getUserInformationById(@RequestHeader(&quot;user_id&quot;) final String id){
        UserDto userDto = userInformationService.getUserInformationById(id);
        if (userDto==null)
            return ResponseEntity.notFound().build();
        return ResponseEntity.ok(userDto);
    }

Why is failing on production but not in local? another similar endpoints of the same class work fine, what am I missing?

答案1

得分: 1

我通过从标题中删除下划线解决了这个问题,我使用了 userId,然后它就正常工作了。

在这里 你可以阅读更多相关信息。

英文:

I solved the issue by removing the underscore from the headers, I used userId

instead, and it worked.

Here you can read more about it

huangapple
  • 本文由 发表于 2023年2月16日 03:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定