PathVariable in SpringBoot with slashes in URL
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Related reading
- Patterns/Principles for thread-safe queues and master/worker program in Java
- peer to peer System with remote method invocation(rmi)
- Performance - Spring Boot - Server Response Time
- Performance Apache HttpAsyncClient vs multi-threaded URLConnection
- Performance issue Java vs C
- Performance Issue with Spring Websocket, RabbitMQ and STOMP
- performance of int Array vs Integer Array
- Performance of synchronize section in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.