0

I have a need to have a single function in a controller map to multiple urls differing by only a few characters. url1: npp/v0/membership url2: npp/hcl/v2/membership

  1. Is there a way to only allow these 2 strings?
  2. What if I want to allow anything pattern?

Thanks.

Jack Peng
  • 576
  • 5
  • 19

1 Answers1

2

Just allowing the two strings would mean you want to use the "path" attribute of the @RequestMapping as it allows an array of Strings.

@RequestMapping(path = {"/npp/v0/membership", "/npp/hcl/v2/membership"})
public void membership() { }

If you want to wildcard you just need to use Ant expressions:

@RequestMapping(path = "/npp/**")
public void npp() { }

If you want the details of the path that was used you would need to look at the HttpServletRequest object that you can have passed into the method.

Shawn Clark
  • 3,330
  • 2
  • 18
  • 30
  • Can I do @RequestMapping(path = "/npp/**/membership") to make sure it ends with "membership" too? – Jack Peng Aug 10 '16 at 05:41
  • 1
    Haven't tried it before. My guess though is it should work if it is truely using the [AntPathMatcher](http://docs.spring.io/autorepo/docs/spring-framework/current/javadoc-api/org/springframework/util/AntPathMatcher.html) – Shawn Clark Aug 10 '16 at 05:54