1. for frontEnd

 1 export class HttpStatusCode {
 2   static OK = 200;
 3   static CREATED = 201;
 4   static ACCEPTED = 202;
 5   static NO_CONTENT = 204;
 6 
 7   static MOVE_PERMANENTLY = 301;
 8   static FOUND = 302;
 9   static SEE_OTHER = 303;
10   static NOT_MODIFIED = 304;
11   static TEMPORARY_REDIRECT = 307;
12   static BAD_REQUEST = 400;
13   static UNAUTHORIZED = 401;
14   static FORBIDDEN = 403;
15   static NOT_FOUND = 404;
16   static METHOD_NOT_ALLOWED = 405;
17   static NOT_ACCEPTABLE = 406;
18   static PRECONDITION_FAILED = 412;
19   static UNSUPPORTED_MEDIA_TYPE = 415;
20 
21   static INTERNAL_SERVER_ERROR = 500;
22   static NOT_IMPLEMENTED = 501;
23 }

2. for Java

  1 import java.io.Serializable;
  2 
  3 /**
  4  * <a href="https://restfulapi.net/http-status-codes/">HTTP Status Codes</a>
  5  * relations-graph form <a href="https://stackoverflow.com/questions/3297048/403-forbidden-vs-401-unauthorized-http-responses/14713094#14713094">stack overflow</a>
  6  *
  7  *    Resource exists ?
  8  *     |       |
  9  *  NO |       | YES
 10  *     v       v
 11  *    404      Is logged-in (authenticated) ?
 12  *    or         |              |
 13  *    401     NO |              | YES
 14  *    403        |              |
 15  *               v              v
 16  *               401            Can access resource, permissions (authorized) ?
 17  *          (404 no reveal)      |            |
 18  *              or 301        NO |            | YES
 19  *              redirect         |            |
 20  *              to login         v            v
 21  *                               403          OK 200, 301, ...
 22  *                       (or 404: no reveal)
 23  *
 24  * </div>
 25  *
 26  * @author pancc
 27  * @version 1.0
 28  * @date 2019/6/12 13:56
 29  */
 30 @SuppressWarnings("unused")
 31 public class HttpStatusCode implements Serializable {
 32     private static final long serialVersionUID = -8658963234424562312L;
 33     /**
 34      * <p>
 35      * It indicates that the REST API successfully carried out whatever action the client requested,
 36      * and that no more specific code in the 2xx series is appropriate.<br>
 37      * Unlike the 204 status code, a 200 response should include a response body.
 38      * The information returned with the response is dependent on the method used in the request, for example:
 39      * </p>
 40      * <ul>
 41      * <li>GET an entity corresponding to the requested resource is sent in the response;</li>
 42      * <li>HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;</li>
 43      * <li>POST an entity describing or containing the result of the action;</li>
 44      * <li>TRACE an entity containing the request message as received by the end server.</li>
 45      * </ul>
 46      */
 47     public static final Integer OK = 200;
 48     /**
 49      * <p>
 50      * A REST API responds with the 201 status code whenever a resource is created inside a collection.
 51      * There may also be times when a new resource is created as a result of some controller action,
 52      * in which case 201 would also be an appropriate response.<br>
 53      * The newly created resource can be referenced by the URI(s) returned in the entity of the response,
 54      * with the most specific URI for the resource given by a Location header field. <br>
 55      * The origin server MUST create the resource before returning the 201 status code.
 56      * If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.
 57      * </p>
 58      */
 59     public static final Integer CREATED = 201;
 60     /**
 61      * <p>A 202 response is typically used for actions that take a long while to process.
 62      * It indicates that the request has been accepted for processing, but the processing has not been completed.
 63      * The request might or might not be eventually acted upon, <br>or even maybe disallowed when processing occurs. <br>
 64      * Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process
 65      * that is only run once per day) without requiring that the user agent’s connection to the server persist
 66      * until the process is completed. <br>
 67      * The entity returned with this response SHOULD include an indication of the request’s current status
 68      * and either a pointer to a status monitor (job queue location) or some estimate of when the user can expect the request to be fulfilled.
 69      * </p>
 70      */
 71     public static final Integer ACCEPTED = 202;
 72     /**
 73      * <p> The 204 status code is usually sent out in response to a PUT, POST,
 74      * or DELETE request when the REST API declines to send back any status message or representation in the response message’s body. <br>
 75      * An API may also send 204 in conjunction with a GET request to indicate that the requested resource exists, but has no state representation to include in the body.<br>
 76      * If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent.
 77      * This response is primarily intended to allow input for actions to take place without causing a change to the user agent’s active document view,
 78      * although any new or updated metainformation SHOULD be applied to the document currently in the user agent’s active view. <br>
 79      * The 204 response MUST NOT include a message-body and thus is always terminated by the first empty line after the header fields.
 80      * </p>
 81      */
 82     public static final Integer NO_CONTENT = 204;
 83     /**
 84      * <p>
 85      * The 301 status code indicates that the REST API’s resource model has been significantly redesigned and a new permanent URI has been assigned to the client’s requested resource.
 86      * The REST API should specify the new URI in the response’s Location header and all future requests should be directed to the given URI.<br>
 87      * You will hardly use this response code in your API as you can always use the API versioning for new API while retaining the old one.
 88      * </p>
 89      */
 90     public static final Integer MOVE_PERMANENTLY = 301;
 91     /**
 92      * <p>
 93      * The HTTP response status code 302 Found is a common way of performing URL redirection.
 94      * An HTTP response with this status code will additionally provide a URL in the location header field.
 95      * The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the location field. <br>
 96      * Many web browsers implemented this code in a manner that violated this standard, changing the request type of the new request to GET,
 97      * regardless of the type employed in the original request (e.g. POST). RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request.
 98      * The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client.
 99      * </p>
100      */
101     public static final Integer FOUND = 302;
102     /**
103      * <p>
104      * A 303 response indicates that a controller resource has finished its work, but instead of sending a potentially unwanted response body, it sends the client the URI of a response resource.
105      * This can be the URI of a temporary status message, or the URI to some already existing, more permanent, resource.<br>
106      * Generally speaking, the 303 status code allows a REST API to send a reference to a resource without forcing the client to download its state. Instead,
107      * the client may send a GET request to the value of the Location header.<br>
108      * The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.
109      * </p>
110      */
111     public static final Integer SEE_OTHER = 303;
112     /**
113      * <p>
114      * This status code is similar to 204 (“No Content”) in that the response body must be empty. The key distinction is that 204 is used when there is nothing to send in the body,
115      * whereas 304 is used when the resource has not been modified since the version specified by the request headers If-Modified-Since or If-None-Match.<br>
116      * In such case, there is no need to retransmit the resource since the client still has a previously-downloaded copy.<br>
117      * Using this saves bandwidth and reprocessing on both the server and client, as only the header data must be sent and received in comparison to the entirety of the page
118      * being re-processed by the server, then sent again using more bandwidth of the server and client.
119      * </p>
120      */
121     public static final Integer NOT_MODIFIED = 304;
122     /**
123      * <p>
124      * A 307 response indicates that the REST API is not going to process the client’s request.
125      * Instead, the client should resubmit the request to the URI specified by the response message’s Location header. However, future requests should still use the original URI.<br>
126      * A REST API can use this status code to assign a temporary URI to the client’s requested resource. For example, a 307 response can be used to shift a client request over to another host.<br>
127      * The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD,
128      * the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s). If the 307 status code is received in response to a request other than GET or HEAD,
129      * the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
130      * </p>
131      */
132     public static final Integer TEMPORARY_REDIRECT = 307;
133     /**
134      * <p>
135      * 400 is the generic client-side error status, used when no other 4xx error code is appropriate. Errors can be like malformed request syntax, invalid request message parameters, or deceptive request routing etc.<br>
136      * The client SHOULD NOT repeat the request without modifications.
137      * </p>
138      */
139     public static final Integer BAD_REQUEST = 400;
140     /**
141      * <p>
142      * A 401 error response indicates that the client tried to operate on a protected resource without providing the proper authorization. It may have provided the wrong credentials or none at all.
143      * The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.<br>
144      * The client MAY repeat the request with a suitable Authorization header field. If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.
145      * If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response,
146      * since that entity might include relevant diagnostic information.
147      *
148      * </p>
149      */
150     public static final Integer UNAUTHORIZED = 401;
151     /**
152      * <p>
153      * A 403 error response indicates that the client’s request is formed correctly, but the REST API refuses to honor it i.e. the user does not have the necessary permissions for the resource.
154      * A 403 response is not a case of insufficient client credentials; that would be 401 (“Unauthorized”).<br>
155      * Authentication will not help and the request SHOULD NOT be repeated. Unlike a 401 Unauthorized response, authenticating will make no difference.
156      *
157      *
158      * </p>
159      */
160     public static final Integer FORBIDDEN = 403;
161     /**
162      * <p>
163      * The 404 error status code indicates that the REST API can’t map the client’s URI to a resource but may be available in the future. Subsequent requests by the client are permissible.<br>
164      * No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism,
165      * that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused,
166      * or when no other response is applicable.
167      *
168      * </p>
169      */
170     public static final Integer NOT_FOUND = 404;
171     /**
172      * <p>
173      * The API responds with a 405 error to indicate that the client tried to use an HTTP method that the resource does not allow. For instance, a read-only resource could support only GET and HEAD,
174      * while a controller resource might allow GET and POST, but not PUT or DELETE.  <br>
175      * A 405 response must include the Allow header, which lists the HTTP methods that the resource supports. For example:<br>
176      * <code>
177      * Allow: GET, POST
178      * </code>
179      *
180      *
181      * </p>
182      */
183     public static final Integer METHOD_NOT_ALLOWED = 405;
184     /**
185      * <p>
186      * The 406 error response indicates that the API is not able to generate any of the client’s preferred media types, as indicated by the Accept request header.
187      * For example, a client request for data formatted as application/xml will receive a 406 response if the API is only willing to format data as <code>application/json</code>.<br>
188      * If the response could be unacceptable, a user agent SHOULD temporarily stop receipt of more data and query the user for a decision on further actions.
189      *
190      * </p>
191      */
192     public static final Integer NOT_ACCEPTABLE = 406;
193     /**
194      * <p>
195      * The 412 error response indicates that the client specified one or more preconditions in its request headers, effectively telling the REST API to carry out its request only if certain conditions were met.
196      * A 412 response indicates that those conditions were not met, so instead of carrying out the request, the API sends this status code.
197      *
198      * </p>
199      */
200     public static final Integer PRECONDITION_FAILED = 412;
201     /**
202      * <p>
203      * The 415 error response indicates that the API is not able to process the client’s supplied media type, as indicated by the Content-Type request header.
204      * For example, a client request including data formatted as application/xml will receive a 415 response if the API is only willing to process data formatted as application/json.<br>
205      * For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.
206      *
207      * </P>
208      */
209     public static final Integer UNSUPPORTED_MEDIA_TYPE = 415;
210     /**
211      * <p>
212      * 500 is the generic REST API error response. Most web frameworks automatically respond with this response status code whenever they execute some request handler code that raises an exception.<br>
213      * A 500 error is never the client’s fault and therefore it is reasonable for the client to retry the exact same request that triggered this response, and hope to get a different response.<br>
214      * API response is the generic error message, given when an unexpected condition was encountered and no more specific message is suitable.
215      *
216      * </p>
217      */
218     public static final Integer INTERNAL_SERVER_ERROR = 500;
219     /**
220      * <p>
221      * The server either does not recognize the request method, or it lacks the ability to fulfill the request. Usually, this implies future availability (e.g., a new feature of a web-service API).
222      * </p>
223      */
224     public static final Integer NOT_IMPLEMENTED = 501;
225 
226 }
HttpStatusCode.class

 check this at gist

posted on 2019-06-17 14:30  四维胖次  阅读(252)  评论(0编辑  收藏  举报