I have a @RestController below as below. The method getTrain(long) is supposed to be picked up for URLs http://localhost:8080/trains/1 but instead it's picking up getTrains(). The other URLs work fine as expected. I am not sure if I am missing or not understanding something. I also looked at Spring request mapping to a different method for a particular path variable value
and it helped a bit.
Requirements: 1. /trains [POST] - add train 2. /trains [GET] - get all trains 3. /trains/{trainId} - get train by id
@RestController
public 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);
}
}
"/{trainId:\\d+}by appending a/?