228

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

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

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.

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

kryger
  • 12,906
  • 8
  • 44
  • 65
luksmir
  • 3,104
  • 5
  • 22
  • 38
  • 2
    Read this article: http://codeflex.co/java-spring-rest-api-with-empty-or-optional-parameters/ – ybonda Mar 29 '18 at 11:08

4 Answers4

279

Before Java 8 and Spring 5 (but works with Java 8+ and Spring 5+ too)

You need to give required = false for name and password request parameters as well. That's because, when you provide just the logout parameter, it actually expects for name and password because they are still "implicitly" mandatory.

It worked when you just gave name and password because logout wasn't a mandatory parameter thanks to required = false already given for logout.

Update for Java 8 and Spring 5 (and above)

You can now use the Optional class from Java 8 onwards to make the parameters optional.

@RequestMapping (value = "/path", method = RequestMethod.GET)
public String handleRequest(@RequestParam("paramName") Optional<String> variableName) {
    String paramValue = variableName.orElse("");
    // use the paramValue
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
193

As part of Spring 4.1.1 onwards 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=false with Optional for your 3 params logout, name, password:

@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 {...}
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • 2
    @VibhavChaddha you can use something like this: if (idOfUser.isPresent()){ System.out.println("idOfUser: "+ idOfUser.get()); } – Cassio Seffrin Apr 09 '17 at 21:47
  • 17
    Intellij warning: 'Optional' used as type for parameter 'requestedTimelineStart' less... (Strg+F1) Inspection info: Reports any uses of java.util.Optional, java.util.OptionalDouble, java.util.OptionalInt, java.util.OptionalLong or com.google.common.base.Optional as the type for a field or a parameter. Optional was designed to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result". Using a field with type java.util.Optional is also problematic if the class needs to be Serializable, which java.util.Optional is not. – PeMa Oct 04 '18 at 09:07
  • 2
    This should be the new correct answer with Java 8 I believe. – java-addict301 Oct 16 '19 at 12:15
  • 4
    @PeMa it's a controversial warning. Personally I think it's much clearer to use `Optional` as parameters instead of `null` checks. Yes, parameters can be `null` itself, but returned `Optional` value can also be `null`. Especially in this case when Spring handles null parameters internally when wrapping it in Optional. – chill appreciator Jul 20 '20 at 23:37
43

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

@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 {...}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 1
    what will happen when someone passes both logout, name and password to the URL ? Just read the documentation, it says `!myParam style expressions indicate that the * specified parameter is not supposed to be present in the request.` got to try. – ring bearer Sep 28 '16 at 06:22
  • 2
    It will find the best match, it probably will try to use the `handleLogin` else it will give an exception stating no mapping can be found. – M. Deinum Sep 28 '16 at 06:51
  • 3
    Just one note: from the security perspective logout should only accept POST requests, so there _should_ be 2 methods and it doesn't make any sense to keep their URL the same then. – FlasH from Ru Apr 13 '17 at 06:05
0

In case someone is looking for mapping Optional parameters with Pojo, same can be done like below.

@RequestMapping (value = "/submit/id/{id}", method = RequestMethod.GET,   
 produces="text/xml")
public String showLoginWindow(@PathVariable("id") String id,
                              LoginRequest loginRequest,
                              @ModelAttribute("submitModel") SubmitModel model,
                              BindingResult errors) throws LoginException {...}
@Data
@NoArgsConstructor
//@AllArgsConstructor - Don't use this
public class LoginRequest {
    private Optional<String> logout = Optional.empty();
    private Optional<String> username = Optional.empty();
    private Optional<String> password = Optional.empty();
}

Note: Do not use @AllArgsConstructor on POJO else it will initialize the fields as null.

Manish Bansal
  • 2,400
  • 2
  • 21
  • 37