1. 들어가며
API를 설계하는 과정에서 URL의 구성 방식은 사용자와의 인터랙션을 결정짓는 중요한 요소입니다. 특히, RESTful API를 설계할 때, 리소스의 식별과 검색 조건 등을 URL에 어떻게 표현할지는 매우 중요한 설계 결정 중 하나입니다.
API 명세서를 작성하던중 Path Variable과 Query Parameter의 정확한 차이점에 대하여 알고자 이 글을 작성합니다..
Path Variable과 Query Parameter의 공통점은 유동적인 값을 전달하기 위하여 URL로 넘겨진다는 것입니다.
Path Variable 또는
- path variable
- path parameter
- paramter
- URL parameter
Query Parameter 또는
- query string
- query parameter
- query
- URL query
2. Path Variable
Path Variable은 URL의 경로 부분에 변수를 포함시키는 방식으로, 주로 리소스의 식별자로 사용됩니다. 이 방식은 리소스 간의 계층적 관계를 명확하게 표현할 수 있으며, RESTful API의 주소 체계에 잘 맞습니다.
/users/12345
- 여기서 /users는 사용자 정보에 대한 리소스를 나타냅니다.
- 12345는 특정 사용자의 고유 식별자를 나타내는 Path Variable입니다.
- 이 URL은 ID가 12345인 사용자의 정보를 요청하고자 할 때 사용됩니다.
3. Query Parameter
Query Parameter는 URL의 끝에 ?와 함께 추가되며, key=value 형태로 표현됩니다. 주로 검색, 필터링, 정렬 등의 추가적인 정보를 제공할 때 사용됩니다. Query Parameter는 리소스 자체보다는 리소스에 대한 요청을 세밀하게 조정할 때 유용합니다.
/users?age=30&country=Korea
- 이 URL은 나이가 30이며, 국가가 Korea인 사용자를 검색하고자 할 때 사용됩니다.
- age=30과 country=Korea는 Query Parameter에 해당하며, 사용자를 필터링하는 조건을 제공합니다.
4. 차이점
- 용도
- Path Variable은 주로 리소스의 식별에 사용되며, URL의 구조적인 부분에 포함됩니다.
- Query Parameter는 리소스에 대한 추가적인 조작(검색, 필터링, 정렬 등)을 지정할 때 사용됩니다.
- 표현 방식
- Path Variable은 URL의 경로에 {중괄호}안에 들어가며, /로 구분됩니다.
- Query Parameter는 URL의 끝에 ?로 시작해 &로 구분됩니다.
- 선택적 사용
- Path Variable은 URL의 필수적인 부분으로, 해당 리소스를 명확히 식별하기 위해 사용됩니다.
- Query Parameter는 선택적으로 사용되며, 같은 리소스에 대한 다양한 요청을 표현할 수 있습니다.
[Reference]
'Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot]스프링 부트에서 ddl-auto 사용 방법 (0) | 2024.06.03 |
---|---|
[Spring Boot] Lombok 어노테이션 정리 (0) | 2024.05.23 |
[Spring Boot] Access Token & Refresh Token (0) | 2024.02.20 |
[Spring Boot] JWT(Json Web Token) - 웹의 안전한 인증 방식 (0) | 2024.02.20 |
[Spring Boot] Lombok 사용법 (0) | 2024.02.19 |