이 페이지에서는 endpoints 라이브러리에서 제공되는 예외와 Cloud Endpoints Frameworks에서 지원되는 HTTP 상태 코드를 보여줍니다.
사용자의 API 요청 성공 또는 실패 여부를 나타낼 때는 공통 HTTP 상태 코드를 사용하려는 경우가 많습니다. 예를 들어 사용자가 존재하지 않는 개체를 가져오려고 하면 HTTP 404 상태 코드를 전송하여 ID가 entityId인 개체가 존재하지 않는다고 알릴 수 있습니다.
다음과 같이 endpoints 라이브러리에서 제공되는 예외를 발생시켜서 공통 HTTP 상태 코드를 전송할 수 있습니다.
thrownewNotFoundException(user.getEmail());
Endpoints Frameworks에서 제공되는 예외
Endpoints Frameworks는 특정 HTTP 상태 코드에 따라 다음과 같은 예외를 제공합니다.
Cloud Endpoints Frameworks에서는 API 응답에서 HTTP 상태 코드 일부가 지원됩니다. 지원되는 코드는 다음 표에 설명되어 있습니다.
HTTP 상태 코드
지원
HTTP 2xx
API 메서드가 성공적으로 반환되면 일반적으로 Endpoints Frameworks에서 HTTP 200이 사용됩니다. API 메서드의 응답 유형이 void이거나, 혹은 API 메서드의 반환 값이 null이면 204가 대신 설정됩니다.
HTTP 3xx
HTTP 3xx 코드는 지원되지 않습니다. HTTP 3xx 코드를 사용하면 HTTP 404 응답이 발생합니다.
HTTP 4xx
다음 HTTP 4xx 코드만 지원됩니다.
400
401
403
404
409
410
412
413
다음을 제외하고 다른 모든 HTTP 4xx 코드는 404 오류로 반환됩니다.
405는 501로 반환됨
408는 503로 반환됨
HTTP 5xx
모든 HTTP 5xx 상태 코드는 클라이언트 응답에서 HTTP 503으로 변환됩니다.
고유한 예외 클래스 만들기
다른 HTTP 상태 코드에 대해 다른 예외 클래스를 만들려면 com.google.api.server.spi.ServiceException을 서브클래스로 만들어야 합니다. 다음 스니펫은 HTTP 408 상태 코드를 나타내는 예외 클래스의 생성 방법을 보여줍니다.
importcom.google.api.server.spi.ServiceException;// Exceptions must inherit from ServiceExceptionpublicclassRequestTimeoutExceptionextendsServiceException{publicRequestTimeoutException(Stringmessage){super(408,message);}}
지원되지 않는 HTTP 상태 코드 사용
위의 지원 목록에 없는 HTTP 상태 코드도 사용할 수 있습니다. 단, 이 경우 API에 액세스하는 클라이언트 라이브러리에 의도하지 않은 결과가 발생할 수 있습니다. 지원되지 않는 오류 코드를 사용하려면 먼저 이전 섹션에서 설명한 대로 커스텀 예외 클래스를 만든 후 새로운 서블릿 초기화 매개변수인 enableExceptionCompatibility를 EndpointsServlet에 추가합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eThe \u003ccode\u003eendpoints\u003c/code\u003e library provides exceptions that correspond to common HTTP status codes, allowing developers to indicate the success or failure of API requests.\u003c/p\u003e\n"],["\u003cp\u003eEndpoints Frameworks offers built-in exception classes for HTTP status codes like \u003ccode\u003e400\u003c/code\u003e, \u003ccode\u003e401\u003c/code\u003e, \u003ccode\u003e403\u003c/code\u003e, \u003ccode\u003e404\u003c/code\u003e, \u003ccode\u003e409\u003c/code\u003e, \u003ccode\u003e500\u003c/code\u003e, and \u003ccode\u003e503\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eHTTP \u003ccode\u003e200\u003c/code\u003e and \u003ccode\u003e204\u003c/code\u003e are supported for successful API methods, while HTTP 3xx codes are not supported and will result in an HTTP \u003ccode\u003e404\u003c/code\u003e response.\u003c/p\u003e\n"],["\u003cp\u003eWhile a subset of HTTP 4xx codes are directly supported, many others are converted to \u003ccode\u003e404\u003c/code\u003e, and all HTTP 5xx codes are converted to \u003ccode\u003e503\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can create custom exception classes for unsupported HTTP status codes by subclassing \u003ccode\u003ecom.google.api.server.spi.ServiceException\u003c/code\u003e and enabling \u003ccode\u003eenableExceptionCompatibility\u003c/code\u003e in the \u003ccode\u003eEndpointsServlet\u003c/code\u003e configuration.\u003c/p\u003e\n"]]],[],null,["# Exceptions and HTTP status codes\n\nThis page lists the exceptions provided by the `endpoints` library as well as\nHTTP status codes supported by Cloud Endpoints Frameworks.\n\nIn many situations, you might want to use common HTTP status codes to indicate\nthe success or failure of a user's API request. For example, if a user is\nattempting to retrieve an entity which doesn't exist, you might want to send an\nHTTP `404` status code to say that no entity exists with ID: `entityId`.\n\nYou can send common HTTP status codes by throwing an exception provided\nby the `endpoints` library as follows: \n\n throw new NotFoundException(user.getEmail());\n\nExceptions provided by Endpoints Frameworks\n-------------------------------------------\n\nEndpoints Frameworks provides the following exceptions, corresponding\nto specific HTTP status codes:\n\nSupported HTTP status codes\n---------------------------\n\nCloud Endpoints Frameworks supports a subset of HTTP status codes\nin API responses. The following table describes the supported codes.\n\n| **Important:** Don't use custom exception classes to return HTTP 2xx codes. Endpoints Frameworks doesn't support returning HTTP `201` or any other 2xx codes except HTTP `200` and HTTP `204` as described in the preceding table.\n\nCreating your own exception classes\n-----------------------------------\n\nIf you want to create other exception classes for other HTTP status codes, you\nneed to subclass `com.google.api.server.spi.ServiceException`. The\nfollowing snippet shows how to create an exception class that represents an\nHTTP `408` status code: \n\n import com.google.api.server.spi.ServiceException;\n\n // Exceptions must inherit from ServiceException\n public class RequestTimeoutException extends ServiceException {\n public RequestTimeoutException(String message) {\n super(408, message);\n }\n }\n\n| **Important:** An uncaught exception in your application results in an HTTP `503` error from your Cloud Endpoints API, unless it extends `com.google.api.server.spi.ServiceException`.\n\nUsing unsupported HTTP status codes\n-----------------------------------\n\nIt is possible to use HTTP status codes that aren't in the preceding supported\nlist. Note that if you decide to do so, it might have unintended consequences\nfor client libraries that access the API. To use unsupported error codes,\ncreate your own exception class, as described in the previous section, and\nadd a new servlet initialization parameter `enableExceptionCompatibility` to\n`EndpointsServlet`: \n\n \u003cservlet\u003e\n \u003cservlet-name\u003ecom.google.api.server.spi.EndpointsServlet\u003c/servlet-name\u003e\n \u003cservlet-class\u003ecom.google.api.server.spi.EndpointsServlet\u003c/servlet-class\u003e\n \u003cinit-param\u003e\n \u003cparam-name\u003eservices\u003c/param-name\u003e\n \u003cparam-value\u003e...\u003c/param-value\u003e\n \u003c/init-param\u003e\n \u003cinit-param\u003e\n \u003cparam-name\u003eenableExceptionCompatibility\u003c/param-name\u003e\n \u003cparam-value\u003etrue\u003c/param-value\u003e\n \u003c/init-param\u003e\n \u003c/servlet\u003e"]]