使用Path变量为其他URL选择默认请求映射方法

时间:2018-08-30 05:37:13

标签: java spring spring-mvc request-mapping

我下面有一个@RestController,如下所示。应该为网址getTrain(long)选择方法http://localhost:8080/trains/1,但要选择getTrains()。其他URL可以正常工作。我不确定我是否想念还是不明白。我还查看了Spring request mapping to a different method for a particular path variable value ,它有所帮助。

要求: 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);
    }
}

2 个答案:

答案 0 :(得分:1)

您应在映射请求中添加 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);
        }

    }

答案 1 :(得分:0)

@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。