【问题标题】:Request Method GET is Not Supported in the Rest APIRest API 不支持请求方法 GET
【发布时间】:2018-12-24 04:01:17
【问题描述】:

我正在开发一个 java springboot 项目以及其他 api,我需要在其中一个 uri 中传递参数,当我使用它时出现“不支持请求方法 GET”错误

  //@GetMapping("logs/date?from={from}&to={to}")
  @RequestMapping(value="logs/date?from={from}&to={to}",method=RequestMethod.Get)
  public list getLogs(@RequestParam(value="from") String from,@RequestParam("to") String to)){....}

当我使用时它可以正常工作

  @RequestMapping(value="logs/date/from={from}&to={to}",method=RequestMethod.Get)
   public list getLogs(@PathVariable(value="from") String from,@PathVariable("to") String to)){....}

但我需要网址有“?”在传递参数之前,所以当我替换时

  @RequestMapping(value="logs/date/from={from}&to={to}",method=RequestMethod.Get)

有了这个

  @RequestMapping(value="logs/date?from={from}&to={to}",method=RequestMethod.Get)

我得到 GET 方法不支持错误。

【问题讨论】:

  • 在未在值映射中指定参数后,我收到“没有可用于 'logs/date/2016-03-03/2016-03-30' 的消息”错误

标签: java rest spring-boot model-view-controller


【解决方案1】:

您编写 RequestMapping 值的方式不正确。您无需在value 字段中写入from={from}&to={to}。正确的方法如下:

@RequestMapping(value="/logs/date",method=RequestMethod.GET) public String getLogs(@RequestParam(value="from") String from,@RequestParam("to") String to)){....}

现在您可以使用 URL 进行 API 调用

http://localhost:8080/log/date?from=fromText&to=toText

【讨论】:

  • 现在可以了,谢谢 Madhu,我在 url 中以错误的方式发送参数
  • 干杯@varun :)
【解决方案2】:

这不是你应该这样做的方式。

您可以在 @PathVariable 的情况下执行此操作,但请求参数应从前端本身定义。

所以不要在控制器或后端这样做。

【讨论】:

  • 到目前为止,我正在使用邮递员检查 uri,所以当我使用“日志/日期?”作为请求映射中的值属性,我从邮递员那里以 json 格式发送起始日期和截止日期我收到错误“无法将字符串的值转换为 long”,用于输入“\date\”
猜你喜欢
  • 2023-04-09
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2021-06-09
  • 2013-04-12
  • 2018-05-26
相关资源
最近更新 更多