I think you want to solve this problem with a two step approach.
Step 1 is write a handler for each of the possible URL formats
Step 2 is to implement the actual functionality separate from the handlers and to call it from the handlers.
Use @RequestParam(required=false) for optional parameters.
// copy from the Lance Java answer.
@RequestMapping(value="/{brand}/{category}/", method=RequestMethod.GET)
public ModelAndView search1(
@PathVariable String brand,
@PathVariable String category,
@RequestParam String minPrice,
@RequestParam String maxPrice,
@RequestParam Integer offer)
{
return actualSearch(brand, category, minPrice, maxPrice, offer);
}
@RequestMapping(value="/{brand}/", method=RequestMethod.GET)
public ModelAndView search1(
@PathVariable String brand,
@RequestParam String minPrice,
@RequestParam String maxPrice,
@RequestParam Integer offer)
{
return actualSearch(brand, null, minPrice, maxPrice, offer);
}
@RequestMapping(value="/brand/{brand}/category/{category}/",
method=RequestMethod.GET)
public ModelAndView search2(
@PathVariable String brand,
@PathVariable String category,
@RequestParam String minPrice,
@RequestParam String maxPrice,
@RequestParam Integer offer)
{
return actualSearch(brand, category, minPrice, maxPrice, offer);
}
@RequestMapping(value="/brand/{brand}/", method=RequestMethod.GET)
public ModelAndView search2(
@PathVariable String brand,
@RequestParam String minPrice,
@RequestParam String maxPrice,
@RequestParam Integer offer)
{
return actualSearch(brand, null, minPrice, maxPrice, offer);
}
private ModelAndView actualSearch(
final String brand,
final String category,
final String minPrice,
final String maxPrice,
final Integer offer)
{
... blah
}
url.matches("\\b"+param+"\\b")but its not giving me exact solution for my problem.@RequestMapping(value = "/{manufacturer}/{deviceType}/{restOfYourParams}", method = RequestMethod.GET)and somewhere later in the code, you could split the restOfYourParams based on a delimiter (if there are more params and you don't know the param names before hand) or use@RequestParamannotation.http://mydomain.com/samsung/orhttp://mydomain.com/samsung/mobilesorhttp://mydomain.com/samsung?min_price=20&max_price=50so I need to map all the cases in controller, I will have to write methods in controller for all cases and I don't want to write methods for all cases. It can be anything.