Java Spring MVC 中的@RequestParam 处理可选参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22373696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:21:09  来源:igfitidea点击:

@RequestParam in Spring MVC handling optional parameters

javaspringspring-mvc

提问by luksmir

Is it possible for a Spring controller to handle both kind of requests?

Spring 控制器是否可以处理这两种请求?

1) http://localhost:8080/submit/id/ID123432?logout=true

1) http://localhost:8080/submit/id/ID123432?logout=true

2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432

2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432

If I define a single controller of the kind:

如果我定义一个单一的控制器:

 @RequestMapping (value = "/submit/id/{id}", method = RequestMethod.GET,   
 produces="text/xml")
public String showLoginWindow(@PathVariable("id") String id,
                              @RequestParam(value = "logout", required = false) String logout,
                              @RequestParam("name") String username,
                              @RequestParam("password") String password,
                              @ModelAttribute("submitModel") SubmitModel model,
                              BindingResult errors) throws LoginException {...}

the HTTP request with "logout" is not accepted.

不接受带有“注销”的 HTTP 请求。

If I define two controllers to handle each request separately, Spring complains with the exception "There is already 'Controller' bean method ... mapped".

如果我定义了两个控制器来分别处理每个请求,Spring 会抱怨“已经有‘控制器’bean 方法......映射”。

采纳答案by SudoRahul

You need to give required = falsefor nameand passwordrequest parameters as well. That's because, when you provide just the logoutparameter, it actually expects for nameand passwordas well as they are still mandatory.

您还需要提供required = falsefornamepasswordrequest 参数。那是因为,当您只提供logout参数时,它实际上期望 forname并且password它们仍然是强制性的。

It worked when you just gave nameand passwordbecause logoutwasn't a mandatory parameter thanks to required = falsealready given for logout.

它在您刚刚给出时起作用,name并且password由于已经给出了 for ,因此logout它不是强制性参数。required = falselogout

回答by M. Deinum

Create 2 methods which handle the cases. You can instruct the @RequestMappingannotation to take into account certain parameters whilst mapping the request. That way you can nicely split this into 2 methods.

创建 2 个处理案例的方法。您可以指示@RequestMapping注释在映射请求时考虑某些参数。这样你就可以很好地把它分成两种方法。

@RequestMapping (value="/submit/id/{id}", method=RequestMethod.GET, 
                 produces="text/xml", params={"logout"})
public String handleLogout(@PathVariable("id") String id, 
        @RequestParam("logout") String logout) { ... }

@RequestMapping (value="/submit/id/{id}", method=RequestMethod.GET, 
                 produces="text/xml", params={"name", "password"})
public String handleLogin(@PathVariable("id") String id, @RequestParam("name") 
        String username, @RequestParam("password") String password, 
        @ModelAttribute("submitModel") SubmitModel model, BindingResult errors) 
        throws LoginException {...}

回答by dimitrisli

As part of Spring 4.1.1onwards you now have full support of Java 8 Optional(original ticket) therefore in your example both requests will go via your single mapping endpoint as long as you replace required=falsewith Optional for your 3 params logout, name, password:

作为以后的一部分,Spring 4.1.1您现在完全支持 Java 8 Optional原始票证),因此在您的示例中,只要您required=false用 Optional替换您的 3 个参数注销、名称、密码,两个请求都将通过您的单个映射端点:

@RequestMapping (value = "/submit/id/{id}", method = RequestMethod.GET,   
 produces="text/xml")
public String showLoginWindow(@PathVariable("id") String id,
                              @RequestParam(value = "logout") Optional<String> logout,
                              @RequestParam("name") Optional<String> username,
                              @RequestParam("password") Optional<String> password,
                              @ModelAttribute("submitModel") SubmitModel model,
                              BindingResult errors) throws LoginException {...}