본문 바로가기
Spring Boot

[Spring] 유니크 키 제약사항을 위반한 경우 HTTP 상태 코드

by palbokdev 2023. 2. 1.

회원가입 시나리오로 예를들면

 

사용자가 입력한 아이디를 

'아이디 중복확인' 버튼을 통해 중복체크 API 호출

 

이때 아이디가 중복된 상태라면 Response로 어떠한 상태 코드를 반환해야 할까?

 

기존에는 409 Conflict(원본과 현재 요청이 충돌)를 사용했지만

422 Unprocessable Entity(요청은 정확하게 이루어졌지만 처리할 수 없음)가 더 적절한 상태코드인것 같다.

 

출처 : https://stackoverflow.com/questions/25015592/whats-the-best-return-code-for-unique-violations

 

What's the best return code for "@unique" violations?

I'm working on a RESTful web API with a Hibernate powered back-end. So far, we've mapped a few error codes. For instance, when @Version validation fails, our API returns HTTP error code 409. Now, we

stackoverflow.com

 

출처 : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses

 

HTTP response status codes - HTTP | MDN

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:

developer.mozilla.org