Home > Software design >  Spring AntPathMatcher with a custom separator behaves strangely
Spring AntPathMatcher with a custom separator behaves strangely

Time:11-14

I face a problem with AntPathMatcher of Spring Boot version 2.5.5. I assume the Ant matcher should return true if the pattern and path are equal and none of them contains Ant-specific characters (i.e. let's say it contains only alphanumeric characters).

The following lines demonstrate the assumption failure:

new AntPathMatcher().match("consent", "consent");             // true
new AntPathMatcher().match("consentreg", "consentreg");       // true

new AntPathMatcher("\\t").match("consent", "consent");        // true
new AntPathMatcher("\\t").match("consentreg", "consentreg");  // false

Do I assume the behavior wrongly? What am I missing here?

CodePudding user response:

You can't use multi-character separators with AntPathMatcher. The path separator is passed to StringUtils.tokenizeToStringArray(String, String, boolean, boolean) as the delimiters argument. This argument is described as "the delimiter characters, assembled as a String (each of the characters is individually considered as a delimiter)". In its current form, your code is telling AntPathMatcher to use \ and t as path separators. As a result, consentreg is split into two strings: consen and reg.

Perhaps you meant to use a tab character as the separator:

new AntPathMatcher("\t").match("consentreg", "consentreg");  // true
  • Related