代码之家  ›  专栏  ›  技术社区  ›  en Peris

缺少请求头“authtoken”调用restapi方法

  •  0
  • en Peris  · 技术社区  · 6 年前

    我有这个restapi方法

    @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

    curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJsb3Blei5hbnRvbmlvODVAZ21haWwuY29tIiwiZXhwIjoxNTk0MTkzNDYwLCJpYXQiOjE1MzM3MsM0NjB9.9pXvdiRMM5fjE4Ur5nqKvwvRLmNWyn6tY6y5fPXOg_BWEW2sJ8vnrLTXPfiA-Sc6Qk2XTwi6FhlIhFEQKip4aQ"  "http://127.0.0.1:1133/canPeris/api/v1/users/menus"
    

    但我得到了这个错误:

       "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....
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Halko Karr-Sajtarevic    6 年前

    @RequestHeader :

    @RequestHeader(value="Authorization") Bearer authToken

        2
  •  0
  •   Rajesh Katadi    6 年前
    Spring MVC provides annotation @RequestHeader that can be used to map controller parameter to request header value .
    
        Can you please change your method to 
    
        @GetMapping(path = "/menus",
                        consumes = "application/json", 
                        produces = "application/json")
        public ResponseEntity<List<MenuPriceSummary>> allMenus(
                    @RequestHeader(value="Authorization") String authToken,
                    HttpServletRequest request) {
    
                  String username = jwtTokenUtil.getUsernameFromToken(authToken);
                User user = userService.findByUserName(username);
                return ResponseEntity.ok(menuService.allMenus(user));
    
            }
    
    You can also make use of Interceptors to validate headers so that other rest endpoints in your application can make use of it .