【问题标题】:Overload Post RequestMapping重载发布请求映射
【发布时间】:2016-07-06 10:21:06
【问题描述】:

我正在尝试重载 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;
}

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    您可以在注解@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 手册。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2019-06-08
      • 2021-10-24
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多