PathVariable in SpringBoot with slashes in URL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot's @PathVariable extracts values from URL path segments, but it breaks when the value itself contains slashes (/). By default, Spring treats each / as a path segment delimiter, so a path variable value like folder/subfolder/file.txt is split across multiple segments instead of being captured as one variable. To handle slashes in path variables, use a regex pattern in the mapping ({path:.+}), use /** with HandlerMethodArgumentResolver, or encode the slashes as %2F.
The Problem
/files/docs/report.pdf does not match /files/{filename} because Spring sees docs and report.pdf as separate path segments.
Fix 1: Regex Pattern {variable:.+}
The .+ regex matches one or more characters including dots, but does not match across / boundaries. This only fixes the problem where Spring strips the file extension (suffix pattern matching), not the slash problem.
Fix 2: Catch-All with /**
Use AntPathMatcher for cleaner extraction:
Fix 3: URL-Encode the Slashes
Encode / as %2F in the client request:
However, many web servers (Tomcat, Apache) reject %2F in path segments by default for security reasons. You must configure the server to allow it:
Fix 4: Use a Request Parameter Instead
Avoid slashes in path variables entirely by using a query parameter:
This is the simplest and most portable solution. Query parameters are not split by /.
Fix 5: Base64 Encode the Path
Use URL-safe Base64 encoding to avoid + and / characters in the encoded output.
Spring Boot 3 / WebFlux
In Spring WebFlux (reactive), the same issue exists:
Common Pitfalls
- Assuming
{variable:.+}captures slashes: The.+regex in@PathVariablematches any character except/. It only prevents Spring from stripping the file extension (e.g.,.json). It does not capture path segments separated by slashes. - Forgetting that Tomcat blocks
%2Fby default: Even if you URL-encode slashes as%2F, Tomcat rejects them with a 400 error for security (path traversal prevention). You must explicitly configureencodedSolidusHandlingto allow them. - Using
request.getRequestURI()without decoding:getRequestURI()returns the raw (encoded) URI. If the path contains%20or other encoded characters, you must decode them. UseURLDecoder.decode(uri, StandardCharsets.UTF_8)or Spring'sHandlerMappingattributes. - Not handling trailing slashes:
/files/docs/and/files/docsmay resolve differently. Spring Boot 3 no longer matches trailing slashes by default. ConfigurePathPatternParserto enable trailing slash matching if needed. - Security risk with path traversal: Accepting arbitrary paths in URLs opens the door to path traversal attacks (
../../etc/passwd). Always validate and sanitize the extracted path before using it to access files. UsePath.normalize()and verify the resolved path stays within the allowed directory.
Summary
@PathVariabledoes not capture slashes — Spring splits on/before matching- Use
/**with manual path extraction for the most reliable approach - Use query parameters (
@RequestParam) to avoid the problem entirely - URL-encoding slashes as
%2Frequires server configuration (Tomcat blocks it by default) - Always validate extracted paths to prevent path traversal attacks
- In Spring Boot 3, trailing slash matching is disabled by default — configure explicitly if needed

