首页 > 解决方案 > 获取http 400错误请求spring mvc

问题描述

我正在开发 Spring mvc 项目。

我必须实现rest api,我现在正在实现的一个api返回http 400错误请求。在其中获取方法请求。另一个 api 为相同的 json 正常工作,但这个 api 返回 HTTP 400

控制器功能

 @RequestMapping(value = "/getSlotsByDateRange/hosid/{hos_id}/from/{start_date}/to/{end_date}" , method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<SlotByDateRange> getSlotsByDateRange(@PathVariable String hos_id,@PathVariable String start_date,@PathVariable String end_date) {
        return new ResponseEntity<SlotByDateRange>(hospitalService.getSlotsByDateRange(hos_id,start_date,end_date),HttpStatus.ACCEPTED);
    }

请求网址:

getSlotsByDateRange/hosid/0009/from/2021-04-15/to/2021-04-22

标签: javarestspring-mvc

解决方案


我刚刚实现了它,它对我有用。

@GetMapping(value = "/getSlotsByDateRange/hosid/{hos_id}/from/{start_date}/to/{end_date}" , produces = "application/json")

对于获取请求,您可以使用@GetMapping 而不是@RequestMapping。由于@GetMapping 本身包含

@RequestMapping(
    method = {RequestMethod.GET}
)

希望这对其他人也有帮助。


推荐阅读