避免使用Regex为HTTP路径映射的不明确的处理程序方法

时间:2018-04-21 03:38:31

标签: java spring

我需要类似的控制器ONe得到Id而另一个得到By名字 两者都使用PathVariables我读了一篇文章,它解释了如何使用正则表达式解决,但它似乎无法正常工作

 @GetMapping(value = "/trucks/{truckId:[0-9]+}")
    @ResponseStatus(HttpStatus.FOUND)
    @ResponseBody
    public final TruckDto getTruckId(@PathVariable(value = "truckId")
                                         final String truckId) {

        LOGGER.debug("test: truckId({})", truckId);
        Truck truck = truckService.getTruckById(Integer.parseInt(truckId));
        return mappingService.map(truck, TruckDto.class);
    }

    /**
     * @return Truck with an average.
     */
    @ResponseStatus(HttpStatus.FOUND)
    @ResponseBody
    @GetMapping(value = "/trucks/{truckCode:[a-zA-Z0-9]+}")
    public final TruckWithAvgPetrolDto getTruckByTruckCode(@PathVariable(value = "truckCode")
                                                     final String truckCode) {

        LOGGER.debug("test: getTruckByTruckCode({})", truckCode);

        TruckWithAvgDto truck = truckService.getTruckByTruckCode(truckCode);

        return mappingService.map(truck, TruckWithAvgPetrolDto.class);
    }

没有让正则表达式都失败我的测试但是在放入正则表达式之后接受数字和字母的方法getTruckByTruckCode传递但是通过truckId得到仍然给出错误的ambigous处理程序方法,我错过了什么或者为什么它不起作用

0 个答案:

没有答案