0

Is it possible to map a spring mvc controller by trailing keyword of a url. e.g, lets suppose I have following urls:

  1. example.com/{cityName}
  2. example.com/{cityName}/{categoryName}
  3. example.com/{cityName}/ping
  4. example.com/{cityName}/{categoryName}/ping

I want to have 3 controller methods. 1st url should be handled by controller "X", 2nd url should be handled by method "Y" and 3rd, 4th url should be handled by single method "Z". This means that any url ending by /ping should be handled by method "Z" only. No matter what is leading content of that url.

Is this feasible in Spring MVC?

2 Answers 2

1

Is it possible to map a spring mvc controller by trailing keyword of a url?

Yes, you can use Ant-style path patterns. Following controller will handle any request to URLs ending with /ping, with arbitrary number of levels:

@RequestMapping(path = "**/ping")
public String Z(HttpServletRequest request) {
    return request.getRequestURI();
}

In order to extract those Path Variables, e.g. cityName and categoryName, you should inject the HttpServletRequest to the method handler.

In addition to URI templates, the @RequestMapping annotation also supports Ant-style path patterns. You can read more on Spring Documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I was looking for
0

It looks like you're asking to match a slash as part of the controller mapping. This isn't supported in Spring MVC, and the maintainers have no plans to add it. As a special case, if you are limited to only two levels of "directories", you can just specify the mappings explicitly as above.

2 Comments

thanks for info. I am not limited to two level directory. It could be 3 or 4, is there any workaround for it then
@Abhi Not a simple one, no. You could comment on that JIRA issue and express your need to be able to do a multi-level match.

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.