重载Post请求映射
问题描述:
我想重载一个Spring控制器的RequestMapping。控制器得到POST作为请求方法,我需要通过它不同的参数来超载它。重载Post请求映射
我怎么能做到这一点,而无需更改网址?
@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;
}
答
您可以使用注释@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
手册。
完美地工作,非常感谢! – Frossy