2

I was wondering if I can customize my url into controller to accept null values

@RequestMapping(
            value = "foo/{y}/{x}",
            method = RequestMethod.GET)
    public ModelAndView doSmth(@PathVariable(value = "x") String x,
                               @PathVariable(value = "y") String y) {
}

Here in my case , X , Y can be empty string . How could I manage smth like that to let this method on the controller to handle this url ?

2
  • what are all combinations of X and Y? Is it possible to split it to many methods - preprocess variables and do logic in some common private method? Commented Jul 12, 2011 at 10:27
  • BTW: stackoverflow.com/questions/5879666/… Commented Jul 12, 2011 at 10:29

2 Answers 2

1

If you want to support optional values, then path variables aren't well suited. Request parameters (using @RequestParam) are a better idea, since they easily support optional values using the required attribute.

If you really want to support optional path variables, then you need to overload your mappings, i.e.

@RequestMapping(value = "foo/{y}/{x}")
public ModelAndView doSmth(@PathVariable(value = "x") String x, @PathVariable(value = "y") String y) 

@RequestMapping(value = "foo/{y}")
public ModelAndView doSmth(@PathVariable(value = "y") String y) 

@RequestMapping(value = "foo")
public ModelAndView doSmth() 
Sign up to request clarification or add additional context in comments.

6 Comments

The thing is this url contains mulible fields that may contain empty string not null . If I follow this methodology , I will need to write many overloads methods . I need to avoid this !!
@Echo: Yes, that's why I just said use @RequestParam instead.
I am trying to support it using @RequestMapping /foo/${x}/[#if x?has_content]${y} [#else] What I can do to pass empty String [/#if]
So is there any advice how to pass empty string !!
@Echo: You're not listening to me - path variables are not suited to this kind of logic.
|
1

I don't think, that's a good URL. It can be very confusing to users.

Why don't you try this one?

@RequestMapping("foo/x/{x}/y/{y}")
public ModelAndView doSmth(@PathVariable String x, @PathVariable String y)

1 Comment

Thanks. I didn't try it as I have used another methodology but ur solution is interesting to be tried .

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.