0

I want to have one url for two functions. The first without parameters and the second with parameters.

code:

@RestController
public class MainController {
    @GetMapping("/")
    public String search(@RequestParam(value = "search") String... keys) {
        return Arrays.toString(keys);
    }

    @GetMapping("/")
    public String results() {
        return "results!";
    }
}

Right now it keeps throwing me an error. is there a solution for it?

Thanks.

Idan Str
  • 614
  • 1
  • 11
  • 33
  • 1
    use required=false in @RequestParam and check whether your keys is null or not if it's null then use your results logic otherwise use your first method logic. and remove the second mapping – Ajay Garg May 08 '18 at 12:01
  • Refer this https://stackoverflow.com/questions/15853035/create-two-method-for-same-url-pattern-with-different-arguments – pooja patil May 08 '18 at 12:07

1 Answers1

1

You should add a params parameter to the @GetMapping, which will allow the mapping to be limited to requests which include those params

@GetMapping(path = "/", params = {"search"})
public String search(@RequestParam("search") String... keys)

See the documentation for @GetMapping#params.

ptomli
  • 11,730
  • 4
  • 40
  • 68