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

Default request mapping method picked up for other URLs with Path variable

我下面有一个@RestController。应该为 URL http://localhost:8080/trains/1 选择方法 getTrain(long),但它却选择了 getTrains()。其他 URL 按预期工作正常。我不确定我是否遗漏或不理解某些东西。我还查看了 Spring 请求映射到特定路径变量值的不同方法 有点帮助。

要求: 1. /trains [POST] - 添加火车 2. /trains [GET] - 获取所有火车 3. /trains/{trainId} - 通过 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);
    }
}
@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}", method = RequestMethod.GET)
    public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {

        return trainService.getTrain(trainId);
    }
}

这行得通。花括号内的值,即 trainId 应映射到 PathVariable。

您应该在映射请求中添加 value = ""。看看这是否有效

@RestController public class TrainController {

    @Autowired
    private TrainService trainService;

    @RequestMapping(value = "/trains",headers = { "Accept=application/json" }, method = RequestMethod.POST)
    public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {

        return trainService.addTrain(trainDto);
    }

    @RequestMapping(value = "/trains",method = RequestMethod.GET)
    public List<TrainDto> getTrains() throws Exception {

        return trainService.getTrains();
    }

    @RequestMapping(value = "/trains/{trainId}", method = RequestMethod.GET)
    public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {

        return trainService.getTrain(trainId);
    }

}

或者你也可以这样做。

@RestController
@RequestMapping(TrainController.REQUEST_MAPPING_URL)
public class TrainController {

       public static final String REQUEST_MAPPING_URL = "/trains";

        @Autowired
        private TrainService trainService;

        @RequestMapping(value = "/",headers = { "Accept=application/json" }, method = RequestMethod.POST)
        public TrainDto addTrain(@RequestBody TrainDto trainDto) throws Exception {
            return trainService.addTrain(trainDto);
        }

        @RequestMapping(value = "/",method = RequestMethod.GET)
        public List<TrainDto> getTrains() throws Exception {
            return trainService.getTrains();
        }

        @RequestMapping(value = "/{trainId}", method = RequestMethod.GET)
        public TrainDto getTrain(@PathVariable("trainId") long trainId) throws Exception {
            return trainService.getTrain(trainId);
        }

    }