为了账号安全,请及时绑定邮箱和手机立即绑定

为其他带有 Path 变量的 URL 选择的默认请求映射方法

为其他带有 Path 变量的 URL 选择的默认请求映射方法

三国纷争 2021-08-19 21:42:08
我有一个@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);    }}
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 168 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信