我有一個(gè)@RestController如下。該方法getTrain(long)應(yīng)該為 URL 獲取,http://localhost:8080/trains/1但它正在獲取getTrains(). 其他 URL 按預(yù)期工作正常。我不確定我是否遺漏或不理解某些東西。我還查看了Spring 請(qǐng)求映射到特定路徑變量值的不同方法 ,它有所幫助。要求: 1. /trains [POST] - 添加火車 2. /trains [GET] - 獲取所有火車 3. /trains/{trainId} - 通過 id 獲取火車@RestControllerpublic class TrainController { @Autowired private TrainService trainService; @RequestMapping(headers = { "Accept=application/json" }, method = RequestMethod.POST) public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception { return trainService.addTrain(trainDto); } @RequestMapping(method = RequestMethod.GET) public List<TrainDto> getTrains() throws Exception { return trainService.getTrains(); } @RequestMapping(value = "{trainId:\\d+}", method = RequestMethod.GET) public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception { return trainService.getTrain(trainId); }}
為其他帶有 Path 變量的 URL 選擇的默認(rèn)請(qǐng)求映射方法
三國(guó)紛爭(zhēng)
2021-08-19 21:42:08