问题描述
我有这个 RestAPI 方法
I have this RestAPI method
@GetMapping(path = "/menus",
consumes = "application/json",
produces = "application/json")
public ResponseEntity<List<MenuPriceSummary>> allMenus(HttpServletRequest request, @RequestHeader(value="Authorization: Bearer") String authToken) {
String username = jwtTokenUtil.getUsernameFromToken(authToken);
User user = userService.findByUserName(username);
return ResponseEntity.ok(menuService.allMenus(user));
}
我从 curl 调用
卷曲-X GET -H 内容类型:应用/JSON" -H 授权:承载eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJsb3Blei5hbnRvbmlvODVAZ21haWwuY29tIiwiZXhwIjoxNTk0MTkzNDYwLCJpYXQiOjE1MzM3MsM0NjB9.9pXvdiRMM5fjE4Ur5nqKvwvRLmNWyn6tY6y5fPXOg_BWEW2sJ8vnrLTXPfiA-Sc6Qk2XTwi6FhlIhFEQKip4aQ"http://127.0.0.1:1133/canPeris/api/v1/用户/菜单"
但是我收到了这个错误:
But I got this error:
"status":400,"error":"Bad Request","message":"Missing request header 'Authorization: Bearer' for method parameter of type String"'authToken' for method parameter of type String","tr....
推荐答案
你不能那样使用 @RequestHeader
.来自标头的值被 :
拆分并添加到 Map 中,因此每个包含 :
的值都是不可能的.
You can't use @RequestHeader
that way. The values from the headers get split up by :
and added to a Map, so every value containing a :
is impossible.
您必须将注释更改为 @RequestHeader(value="Authorization")
,然后从 authToken
中删除 Bearer
.
You will have to change your annotation to @RequestHeader(value="Authorization")
and then remove the Bearer
from the authToken
.
这篇关于缺少调用 RestAPI 方法的请求标头“authToken"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!