2016-07-06 125 views
0

我想重載一個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; 
} 

回答

1

您可以使用註釋@RequestParamparams選項,如:

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

+0

完美地工作,非常感謝! – Frossy