How to get the request URL in a Spring Boot RestController
Codemia Knowledge Hub ·
Inject HttpServletRequest into your handler method and call getRequestURL() for the full URL or getRequestURI() for just the path. Spring also provides ServletUriComponentsBuilder for building complete URLs (scheme, host and port included) without depending on the servlet API directly.
Reading the URL from HttpServletRequest
Spring injects HttpServletRequest automatically when you declare it as a method parameter:
For a request to http://localhost:8080/api/demo?page=1&size=10, this returns:
Methods for each part of the URL
| Method | Returns | Example Value |
getRequestURL() | Full URL without query string | http://localhost:8080/api/demo |
getRequestURI() | Path portion only | /api/demo |
getScheme() | Protocol | https |
getServerName() | Host name | localhost |
getServerPort() | Port number | 8080 |
getContextPath() | Application context path | /myapp or "" (root) |
getServletPath() | Path matched by the servlet | /api/demo |
getQueryString() | Raw query string (null if none) | page=1&size=10 |
getRemoteAddr() | Client IP address | 127.0.0.1 |
Building URLs with ServletUriComponentsBuilder
ServletUriComponentsBuilder is a cleaner, more testable option that does not need HttpServletRequest injected:
This is the standard pattern for REST APIs that return a Location header after creating a resource. The builder picks up the current request's scheme, host, port and path.
Path variables and query parameters
Spring MVC annotations extract specific parts of the URL so you do not have to parse strings:
| Annotation | Extracts | Example |
@PathVariable | Named segments from the URL path | /users/{id} extracts id |
@RequestParam | Query string parameters | ?page=2 extracts page |
@RequestHeader | HTTP header values | Authorization header |
@CookieValue | Cookie values | Session cookies |
@MatrixVariable | Matrix parameters (semicolon-separated) | /users;role=admin |
Getting the external URL behind a proxy
In production the app usually sits behind a reverse proxy (Nginx, AWS ALB, Cloudflare). The client uses HTTPS on port 443, but the request reaching Spring Boot uses HTTP on port 8080, so getRequestURL() returns the internal URL.
Configure Spring Boot to trust forwarded headers:
And have the proxy send the standard forwarding headers:
Now getRequestURL() and ServletUriComponentsBuilder return the external URL (https://api.example.com/api/demo) instead of the internal one (http://localhost:8080/api/demo).
Getting the URL outside a controller with RequestContextHolder
In a service or utility class, RequestContextHolder gives access to the current request from anywhere in the request-handling thread:
This helps in logging interceptors, audit services or error handlers. It couples those classes to the servlet layer, though, which makes unit testing harder. Passing the URL as a method parameter is cleaner when possible.
Mistakes when reading the request URL
- Using
getRequestURL()behind a reverse proxy withoutforward-headers-strategy. You gethttp://localhost:8080instead of the public URL. - Forgetting that
getQueryString()returnsnull, not an empty string, when there are no query parameters.getRequestURL() + "?" + getQueryString()then produces...?null. - Using
getRequestURL()when you only need the path.getRequestURI()is cheaper and avoids scheme/host/port coupling, which makes testing easier. - Importing
javax.servlet.http.HttpServletRequestin Spring Boot 3.x. Spring Boot 3 uses Jakarta EE, so the import isjakarta.servlet.http.HttpServletRequest. - Logging full URLs with query parameters in production without sanitizing. Query strings may contain tokens, API keys or personally identifiable information.
Related reading
- How to get rid of Incremental annotation processing requested warning?
- How to get Spinner value?
- How to get Spring RabbitMQ to create a new Queue?
- How to get status code from webclient?
- How to get the concrete class name as a string?
- How to get the current date/time in Java
- How to get the current time in YYYY-MM-DD HHMISec.Millisecond format in Java?
- How to get the current working directory in Java?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.