Anil Kumar Pandey
Anil Kumar Pandey

Reputation: 1927

Spring Web MVC: Regex in controller mapping

I want to upgrade my website URL mapping. Previously it was purely querying based like: http://mydomain.com?brand=samsung&category=mobile&min_price=20&max_price=50&offer=10

Now I want to convert my URLs to be more RESTlike. I want it to look like this: http://mydomain.com/samsung/mobiles?min_price=20&max_price=50&offer=10

or in more descriptive format: http://mydomain.com/brand/samsung/category/mobiles?min_price=20&max_price=50&offer=10

I believe that I need to use Regex in the mapping of the spring controller. I am not very good in writing regular expression, but got some useful resources from Google. I read this spring reference document but can't figure out a regex to solve my requirement.

Please give me a simple demonstration or any solution for the problem.

Upvotes: 2

Views: 790

Answers (2)

DwB
DwB

Reputation: 38300

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
}

Upvotes: 2

lance-java
lance-java

Reputation: 27976

If you are using annotations, you could do something like...

@RequestMapping(value="/{brand}/{category}/", method=RequestMethod.GET)
public ModelAndView search(
      @PathVariable String brand, 
      @PathVariable String category, 
      @RequestParam String min_price, 
      @RequestParam String max_price, 
      @RequestParam Integer offer) {

   // do something fantastic
}

Upvotes: 0

Related Questions