簡體   English   中英

Rest API不支持請求方法GET

[英]Request Method GET is Not Supported in the Rest API

我正在與其余api一起在java springboot項目上工作,我需要在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方法不受支持的錯誤。

您編寫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

這不是您應該這樣做的方式。

您可以在@PathVariable情況下執行此操作,但對於請求參數,應從前端本身定義。

因此,請勿在控制器或后端中執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM