Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina elenca le eccezioni fornite dalla libreria endpoints e i codici di stato HTTP supportati da Cloud Endpoints Frameworks.
In molte situazioni, potresti voler utilizzare codici di stato HTTP comuni per indicare
l'esito positivo o negativo di una richiesta API di un utente. Ad esempio, se un utente tenta di recuperare un'entità inesistente, potresti voler inviare un codice di stato HTTP 404 per indicare che non esiste alcuna entità con ID: entityId.
Puoi inviare codici di stato HTTP comuni generando un'eccezione fornita dalla libreria endpoints nel seguente modo:
thrownewNotFoundException(user.getEmail());
Eccezioni fornite da Endpoints Frameworks
Endpoints Frameworks fornisce le seguenti eccezioni, corrispondenti
a codici di stato HTTP specifici:
Cloud Endpoints Frameworks supporta un sottoinsieme di codici di stato HTTP
nelle risposte API. La tabella seguente descrive i codici supportati.
Codici di stato HTTP
Assistenza
HTTP 2xx
HTTP 200 viene in genere presupposto da Endpoints Frameworks se il metodo API viene restituito correttamente. Se il tipo di risposta del metodo API è void o il valore restituito del metodo API è null , viene impostato HTTP 204.
HTTP 3xx
I codici HTTP 3xx non sono supportati. L'utilizzo di qualsiasi codice HTTP 3xx genera una risposta HTTP 404.
HTTP 4xx
Sono supportati solo i seguenti codici HTTP 4xx:
400
401
403
404
409
410
412
413
Tutti gli altri codici HTTP 4xx vengono restituiti come errore 404, ad eccezione dei seguenti:
405 viene restituito come 501
408 viene restituito come 503
HTTP 5xx
Tutti i codici di stato HTTP 5xx vengono convertiti in HTTP 503 nella risposta del client.
Creazione di classi di eccezioni personalizzate
Se vuoi creare altre classi di eccezioni per altri codici di stato HTTP, devi creare una sottoclasse di com.google.api.server.spi.ServiceException. Lo
snippet seguente mostra come creare una classe di eccezioni che rappresenta un
codice di stato HTTP 408:
importcom.google.api.server.spi.ServiceException;// Exceptions must inherit from ServiceExceptionpublicclassRequestTimeoutExceptionextendsServiceException{publicRequestTimeoutException(Stringmessage){super(408,message);}}
Utilizzo di codici di stato HTTP non supportati
È possibile utilizzare codici di stato HTTP non inclusi nell'elenco
precedente. Tieni presente che, se decidi di farlo, potrebbero esserci conseguenze indesiderate
per le librerie client che accedono all'API. Per utilizzare codici di errore non supportati,
crea una classe di eccezioni personalizzata, come descritto nella sezione precedente, e
aggiungi un nuovo parametro di inizializzazione servlet enableExceptionCompatibility a
EndpointsServlet:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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"]]