• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Required request parameter ‘token‘ for method parameter type xxxx is not present 解决方式

武飞扬头像
Jandey.柳
帮助1

        昨天系统有个绑定手机号的功能接口,绑定提交一直报错不好使,报错如下:

  1.  
    GlobalExceptionHandler.exceptionHandler:message:Required request parameter 'token' for method parameter type String is not present, stackTrace:
  2.  
    [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValueInternal(RequestParamMethodArgumentResolver.java:218),
  3.  
    org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:193),
  4.  
    org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:114),
  5.  
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121),
  6.  
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179),
  7.  
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146),
  8.  
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117),
  9.  
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895),
  10.  
    ......

        这个报错很明显是参数传递时出现问题,看接口代码:

  1.  
    @RequestMapping(value = "/binding", method = RequestMethod.POST, produces = "application/json")
  2.  
    @ResponseBody
  3.  
    public Map<?, ?> bindingMobile(@RequestBody UserVo userInfo, @RequestParam(value = "token") String token) {
  4.  
    // 内容省略
  5.  
    }

我看这个参数写法,一阵头大,POST请求,获取参数怎么还用 @RequestParam 注解,这匹配不了啊。我这边查看了请求,发现token参数是在请求头中,于是修改了接口参数:

  1.  
    @RequestMapping(value = "/binding", method = RequestMethod.POST, produces = "application/json")
  2.  
    @ResponseBody
  3.  
    public Map<?, ?> bindingMobile(@RequestBody UserVo userInfo, HttpServletRequest request) {
  4.  
     
  5.  
    // 获取token
  6.  
    String token = request.getHeader("token");
  7.  
     
  8.  
    // 下面内容省略
  9.  
    }

上面修改后参数获取就没问题了,接口功能也好使了。

网上有个博主列出了请求类型和参数的对应表,我觉得挺好的,引用一下:

注解 支持的类型 支持的请求类型 支持的Content-Type 请求示例
@PathVariable url GET 所有 /test/{id}
@RequestParam url GET 所有 /test?id=1
@RequestBody Body POST/PUT/DELETE/PATCH json {“id”:1}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhaiag
系列文章
更多 icon
同类精品
更多 icon
继续加载