2

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.

6
  • "or any other resource link" Asking for resources is considered off-topic on Stack Overflow. Commented Apr 29, 2014 at 13:26
  • @Pshemo I didn't get any good resource for the same in google, That's why I have posted here. Currently I am using regex for exact matching eg: url.matches("\\b"+param+"\\b") but its not giving me exact solution for my problem. Commented Apr 29, 2014 at 13:30
  • I don't understand why you'd need Regex. Can you tell please explain? If you're using Spring annotations you could do something like @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 @RequestParam annotation. Commented Apr 29, 2014 at 13:31
  • @Pshemo Now I hv removed the "off-topic" from my post Commented Apr 29, 2014 at 13:33
  • @prabugp the url can be different, It can be http://mydomain.com/samsung/ or http://mydomain.com/samsung/mobiles or http://mydomain.com/samsung?min_price=20&max_price=50 so 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. Commented Apr 29, 2014 at 13:35

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @DwB for the example code. Finally I am going to use your approach.
0

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
}

7 Comments

what will happen if url will something like this http://mydomain.com/samsung?min_price=20&max_price=50. I think your controller will not map it. Here all the parameters are optional. User can apply the the "brand" before "category" or can directly apply the "min_price" and "max_price" query parameters.
@user2518430 That would be a different handler mapping.
Yes right, that is the actual problem I have to write the different-different mapping in the controller methods. That's why I am asking for any regex which will capable to handle (not all cases)some cases.
value="/{brand}/{category}/" and value="/{category}/{brand}/" will produce same output in my case, but here I am writing two methods for the same, which I don't want to do. I think now I am pretty clear with my problem... right @Bart
@user2518430 I don't see how regular expressions could magically identify a brand or a category. You're probably better off using more explicit mappings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.