我有一个@RestController如下。该方法getTrain(long)应该为 URL 获取,http://localhost:8080/trains/1但它正在获取getTrains(). 其他 URL 按预期工作正常。我不确定我是否遗漏或不理解某些东西。我还查看了Spring 请求映射到特定路径变量值的不同方法 ,它有所帮助。要求: 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); }}
添加回答
举报
0/150
提交
取消