我正在开发一个 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)){....}
但我需要 url 有“?”在传递参数之前,所以当我替换
@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
关于java - Rest API 不支持请求方法 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364641/