Echo
Echo

Reputation: 3029

Spring MVC Url pattern

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 ?

Upvotes: 2

Views: 2266

Answers (2)

Whiteship
Whiteship

Reputation: 1869

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)

Upvotes: 1

skaffman
skaffman

Reputation: 403441

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() 

Upvotes: 1

Related Questions