RequestMapping中的过载Post请求

3

我想要重载Spring控制器的RequestMapping。该控制器使用POST作为请求方法,我需要传递不同的参数来进行重载。

如何在不更改URL的情况下实现这一点?

@RequestMapping(method = RequestMethod.POST, value = "/windparks/import/error")
public ModelAndView handleFileUploadError(Locale locale, @AuthenticationPrincipal SpringUser authenticatedUser, List<RegulationEvent> regList, @RequestParam("regulations") MultipartFile regulations, RedirectAttributes redirectAttributes, @PathVariable String windparkId) throws IOException, WindSpeedInterpolator.TimeSeriesMismatchException, SAXException {
    ModelAndView view = new ModelAndView("uis/windparks/parkdetail");

    view.addObject("failedEvents", regList);
    view.addObject("windparkId", windparkId);


    return view;
}

@RequestMapping(method = RequestMethod.POST, value = "/windparks/import/error")
public ModelAndView handleFileUploadError(Locale locale, @AuthenticationPrincipal SpringUser authenticatedUser, RedirectAttributes redirectAttributes, @PathVariable String windparkId) throws IOException, WindSpeedInterpolator.TimeSeriesMismatchException, SAXException {
    ModelAndView view = new ModelAndView("uis/windparks/parkdetail");

    view.addObject("windparkId", windparkId);


    return view;
}
1个回答

3
你可以在注解 @RequestParam 中使用 params 选项,像这样:

@RequestMapping(method = RequestMethod.POST, value = "/windparks/import/error", params = {"locale", "authenticatedUser", "regList", "regulations", "windparkId"})
public ModelAndView handleFileUploadError(Locale locale, @AuthenticationPrincipal SpringUser authenticatedUser, List<RegulationEvent> regList, @RequestParam("regulations") MultipartFile regulations, RedirectAttributes redirectAttributes, @PathVariable String windparkId) throws IOException, WindSpeedInterpolator.TimeSeriesMismatchException, SAXException {
    ModelAndView view = new ModelAndView("uis/windparks/parkdetail");

    view.addObject("failedEvents", regList);
    view.addObject("windparkId", windparkId);


    return view;
}

@RequestMapping(method = RequestMethod.POST, value = "/windparks/import/error", params = {"locale", "authenticatedUser", "windparkId"})
public ModelAndView handleFileUploadError(Locale locale, @AuthenticationPrincipal SpringUser authenticatedUser, RedirectAttributes redirectAttributes, @PathVariable String windparkId) throws IOException, WindSpeedInterpolator.TimeSeriesMismatchException, SAXException {
    ModelAndView view = new ModelAndView("uis/windparks/parkdetail");

    view.addObject("windparkId", windparkId);


    return view;
}

也许对你没有用,但你可以在Spring手册中查看params的说明。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接