{"id":690,"date":"2015-12-14T16:27:10","date_gmt":"2015-12-14T19:27:10","guid":{"rendered":"https:\/\/www.semeru.com.br\/blog\/?p=690"},"modified":"2022-12-21T10:27:01","modified_gmt":"2022-12-21T13:27:01","slug":"http-status-codes-em-servicos-rest","status":"publish","type":"post","link":"https:\/\/www.semeru.com.br\/blog\/http-status-codes-em-servicos-rest\/","title":{"rendered":"HTTP Status Codes em Servi\u00e7os REST"},"content":{"rendered":"<p>Uma boa pr\u00e1tica ao se desenvolver servi\u00e7os REST \u00e9 retornar status codes adequados a cada opera\u00e7\u00e3o executada. Isso tem uma import\u00e2ncia ainda maior quando se trata de uma exce\u00e7\u00e3o, uma vez que diferentementemente de um servi\u00e7o SOAP uma aplica\u00e7\u00e3o verdadeiramente REST n\u00e3o retorna a exception para o usu\u00e1rio. Sendo assim \u00e9 fortemente recomendado que ao se desenvolver uma API REST se v\u00e1 al\u00e9m dos status code 200 e 500 OK e falha respectivamente.<\/p>\n<figure id=\"attachment_1158\" class=\"thumbnail wp-caption alignnone\" style=\"width: 658px\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-1158\" src=\"https:\/\/www.erudio.com.br\/blog\/wp-content\/uploads\/2015\/12\/HTTP_StatusCodes_By_Group-1024x497.png\" alt=\"HTTP Status Codes por Grupo\" width=\"648\" height=\"315\"><figcaption class=\"caption wp-caption-text\">HTTP Status Codes<\/figcaption><\/figure>\n<p><strong>200 OK<\/strong> &#8211; Request de cria\u00e7\u00e3o ou dele\u00e7\u00e3o executada com sucesso.<\/p>\n<p><strong>201 Created<\/strong> &#8211; Cria\u00e7\u00e3o de uma fila, topico, fila tempor\u00e1ria, t\u00f3pico tempor\u00e1ria, session, producer, consumer, listener, queue browser ou menssagem realizada com sucesso.<\/p>\n<p><strong>204 No Content<\/strong> &#8211; dele\u00e7\u00e3o de uma fila, t\u00f3pico, sess\u00e3o, producer ou listener bem sucedida mas sem retorno de conte\u00fado.<\/p>\n<p><strong>400 Bad Request<\/strong> &#8211; O path informado est\u00e1 em umformato incorreto, um parametro ou valor do corpo da requisi\u00e7\u00e3o n\u00e3o est\u00e1 formatado corretamente ou um par\u00e2metro onbrigat\u00f3rio n\u00e3o foi informado,<br \/>\nou est\u00e1 formatado corretamente mas pode estar eventualmente inv\u00e1lido (por exemplo, o ID informado n\u00e3o exite &#8211; NullPointerException, o conte\u00fado retornado \u00e9 muito grande ou o ID informado j\u00e1 est\u00e1 em uso).<\/p>\n<p><strong>403 Forbidden<\/strong> &#8211; O cliente n\u00e3o tem permiss\u00e3o para executar requisi\u00e7\u00f5es na opera\u00e7\u00e3o em quest\u00e3o.<\/p>\n<p><strong>404 Not Found<\/strong> &#8211; o objeto requisitado pelo path n\u00e3o existe(NullPointerException).<\/p>\n<p><strong>405 Method Not Allowed<\/strong> &#8211; O usu\u00e1rio n\u00e3o tem permiss\u00e3o de acesso ao path.<\/p>\n<p><strong>409 Conflict<\/strong> &#8211; Um objeto j\u00e1 foi criado com as mesmas informa\u00e7\u00f5es.<\/p>\n<p><strong>500 Internal Server Error <\/strong>&#8211; Ocorreu uma falha no servidor, podendo ser desde uma falha no SQL por exemplo.<\/p>\n<p>A pr\u00e1tica de retornar os status code corretos torna seus servi\u00e7os mais amig\u00e1veis ao cliente facilitando a integra\u00e7\u00e3o com outros sistemas. Nos pr\u00f3ximos posts botaremos a m\u00e3o na massa desenvolvendo a nossa propria API\/Servi\u00e7o REST.<\/p>\n<h2>Botando em pr\u00e1tica<\/h2>\n<p>Em breve estar\u00e3o dispon\u00edveis dois posts <a href=\"https:\/\/www.semeru.com.br\/blog\/criando-o-primeiro-endpoint-rest-com-springboot\/\">Criando o primeiro endpoint REST com SpringBoot<\/a> e <a href=\"https:\/\/www.semeru.com.br\/blog\/documentando-aplicacoes-restfull-com-springboot-e-swagger\/\">Documentando aplica\u00e7\u00f5es RESTfull com SpringBoot e Swagger <\/a> com exemplos pr\u00e1ticos.<\/p>\n<h2>Treinamentos relacionados com este post<\/h2>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_rest_spring_java\" target=\"_blank\" rel=\"noopener\"><br \/>\n<img decoding=\"async\" style=\"max-width: 100%;\" title=\"REST API's RESTFul do 0 \u00e0  AWS com Spring Boot 3, Java e Docker\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/07-rest-spring-java.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_rest_asp_net\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" style=\"max-width: 100%;\" title=\"REST API's RESTFul do 0 \u00e0 Azure com ASP.NET Core 5 e Docker\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/01-rest-asp.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_rest_spring_kotlin\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"REST API's RESTFul do 0 \u00e0 AWS com Spring Boot 3, Kotlin e Docker\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/18-rest-spring-kotlin.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_microservices_java\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Microservices do 0 com Spring Cloud, Spring Boot e Docker\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/14-microservices-java.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_microservices-dotnet\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Arquitetura de Microsservi\u00e7os do 0 com ASP.NET, .NET 6 e C#\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/15-microservices-dotnet.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_ms_kotlin\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Microsservi\u00e7os do 0 com Spring Cloud, Kotlin e Docker\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/22-ms-kotlin.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_docker\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Docker do 0 \u00e0 Maestria: Cont\u00eaineres Desmistificados mais 3 B\u00d4NUS\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/09-docker.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_docker_para_aws\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Docker para Amazon AWS Implante Apps Java e .NET com Travis CI\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/10-docker-to-aws.png\"><br \/>\n<\/a><\/p>\n<p><a href=\"https:\/\/pub.erudio.com.br\/kr\/blog_kotlin\" target=\"_blank\" rel=\"noopener\"> <img decoding=\"async\" style=\"max-width: 100%;\" title=\"Kotlin para DEVs Java: Aprenda a Linguagem Padr\u00e3o do Android\" src=\"https:\/\/raw.githubusercontent.com\/leandrocgsi\/blog-images\/main\/20-kotlin.png\"><br \/>\n<\/a><\/p>\n<h2>Refer\u00eancias:<\/h2>\n<p><a title=\"https:\/\/dzone.com\/articles\/a-fun-way-to-explore-http-status-codes-with-a-subw\" href=\"https:\/\/dzone.com\/articles\/a-fun-way-to-explore-http-status-codes-with-a-subw\">DZone<\/a><\/p>\n<p><a title=\"http:\/\/www.javaworld.com\/article\/2073275\/jax-rs-and-http-responses.html\" href=\"http:\/\/www.javaworld.com\/article\/2073275\/jax-rs-and-http-responses.html\">Java World<\/a><\/p>\n<p><a title=\"http:\/\/stackoverflow.com\/questions\/4687271\/jax-rs-how-to-return-json-and-http-status-code-together\" href=\"http:\/\/stackoverflow.com\/questions\/4687271\/jax-rs-how-to-return-json-and-http-status-code-together\">StackOverflow<\/a><\/p>\n<p><a title=\"https:\/\/docs.oracle.com\/cloud\/latest\/messcs_common\/CSMES\/GUID-AAB1EE32-BE4A-4ACC-BEAC-ABA85EB41919.htm#GUID-AAB1EE32-BE4A-4ACC-BEAC-ABA85EB41919\" href=\"https:\/\/docs.oracle.com\/cloud\/latest\/messcs_common\/CSMES\/GUID-AAB1EE32-BE4A-4ACC-BEAC-ABA85EB41919.htm#GUID-AAB1EE32-BE4A-4ACC-BEAC-ABA85EB41919\">Oracle<\/a><\/p>\n<div align=\"right\"><div class=\"sharexyWidgetNoindexUniqueClassName\"><div id=\"shr_79530600\"><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Uma boa pr\u00e1tica ao se desenvolver servi\u00e7os REST \u00e9 retornar status codes adequados a cada opera\u00e7\u00e3o executada. Isso tem uma import\u00e2ncia ainda maior quando se trata de uma exce\u00e7\u00e3o, uma vez que diferentementemente de um servi\u00e7o SOAP uma aplica\u00e7\u00e3o verdadeiramente REST n\u00e3o retorna a exception para o usu\u00e1rio. Sendo assim \u00e9 fortemente recomendado que ao [&#8230;]<\/p>\n<div align=\"right\">\n<div class=\"sharexyWidgetNoindexUniqueClassName\">\n<div id=\"shr_79530600\"><\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[117,102,116,120,121],"tags":[205,191,204,206,207],"_links":{"self":[{"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/posts\/690"}],"collection":[{"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":15,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"predecessor-version":[{"id":1500,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions\/1500"}],"wp:attachment":[{"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.semeru.com.br\/blog\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}