Issue
I'd like to use one URL mapping /all
with request parameters (month
, date
) or without parameters.
I've tried to create two methods, one without parameters:
@RequestMapping(value = "/all", method = RequestMethod.GET)
public CommonResponse getAll() {
}
And one with parameters:
@RequestMapping(value = "/all", method = RequestMethod.GET)
public CommonResponse getByMonth(@RequestParam int month, @RequestParam(required = false) int year) {
}
But i am getting "Ambiguous mapping found" IllegalStateException
. Does Spring have any way to handle this situation?
Note:- Please don't suggest this solution because I have different scenario.
Solution
I got the solution :)
@RequestMapping(value = "/all", method = RequestMethod.GET)
public CommonResponse getAll(@RequestParam(required = false) Optional<Integer> month,
@RequestParam(required = false, defaultValue = "0") int year) {
if (month.isPresent()) {
return getByMonth(month.get, year);
}
return getAll();
}
Answered By - Victory
Answer Checked By - Cary Denson (JavaFixing Admin)