requestURI
2023. 6. 23. 06:36ㆍSpring Framework/Web on Servlet Stack
requestURI
는 HttpServletRequest 객체가 제공하는 요청된 URL 경로의 전체 정보 중, 도메인과 쿼리 파라미터를 제외한 경로 부분을 의미합니다.
✅ requestURI
란?
- 정의:
HttpServletRequest.getRequestURI()
메서드로 얻을 수 있는 값 - 형식:
contextPath + servletPath + pathInfo
String requestURI = request.getRequestURI();
✅ 구성 예시
예를 들어 다음과 같은 요청 URL이 있다고 합시다:
http://localhost:8080/myapp/api/users/123?verbose=true
항목 | 값 | 설명 |
---|---|---|
scheme | http |
프로토콜 |
serverName | localhost |
호스트 이름 |
serverPort | 8080 |
포트 넘버 |
contextPath | /myapp |
웹 애플리케이션의 루트 경로 |
servletPath | /api |
DispatcherServlet의 매핑 경로 |
pathInfo | /users/123 |
서블릿 경로 이후 남은 경로 |
requestURI | /myapp/api/users/123 |
contextPath + servletPath + pathInfo |
queryString | verbose=true |
URL의 쿼리 파라미터 |
✅ 코드 예시
@GetMapping("/hello")
public ResponseEntity<String> hello(HttpServletRequest request) {
String requestURI = request.getRequestURI();
return ResponseEntity.ok("Request URI: " + requestURI);
}
http://localhost:8080/hello
로 요청하면:
Request URI: /hello
http://localhost:8080/myapp/hello?name=John
이라면:
Request URI: /myapp/hello
(쿼리 스트링은 포함되지 않음)
✅ requestURL
과의 차이
메서드 | 설명 |
---|---|
getRequestURI() |
도메인과 쿼리 제외한 전체 경로 |
getRequestURL().toString() |
전체 URL (스킴 + 호스트 + 포트 + URI 포함) |
getQueryString() |
쿼리 파라미터 (key=value ) 만 추출 |
예:
request.getRequestURL() → "http://localhost:8080/myapp/api/users/123"
request.getRequestURI() → "/myapp/api/users/123"
request.getQueryString() → "verbose=true"
✅ 정리
requestURI
란?
사용자가 브라우저에 입력한 URL 중,
contextPath + servletPath + pathInfo로 구성된
도메인과 쿼리 스트링을 제외한 순수한 경로 정보입니다.
Spring이나 서블릿에서 URL 경로에 따라 요청을 라우팅할 때 핵심적인 정보로 사용됩니다.
'Spring Framework > Web on Servlet Stack' 카테고리의 다른 글
HandlerAdapter의 추상화 작업 (0) | 2023.06.23 |
---|---|
contextPath와 servletPath (0) | 2023.06.23 |
Filter (0) | 2023.05.19 |
WebDataBinder (0) | 2023.05.01 |
Servlet (0) | 2023.04.17 |