前言:当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求。
HTTP状态码(HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码。它由 RFC 2616 规范定义的,所有状态码的第一个数字代表了响应的五种状态之一。
1、HttpStatus状态码表
常用HttpStatus状态码
- HttpStatus.OK = 200;
- HttpStatus.BAD_REQUEST = 400;
- HttpStatus.FORBIDDEN = 403;
- HttpStatus.UNAUTHORIZED= 401;
- HttpStatus.NOT_FOUND = 404;
- HttpStatus.REQUEST_TIMEOUT = 408;
- HttpStatus.SERVICE_UNAVAILABLE =500;
HttpStatus = {
//Informational 1xx 信息
'100' : 'Continue', //继续
'101' : 'Switching Protocols', //交换协议
//Successful 2xx 成功
'200' : 'OK', //OK
'201' : 'Created', //创建
'202' : 'Accepted', //已接受
'203' : 'Non-Authoritative Information', //非权威信息
'204' : 'No Content', //没有内容
'205' : 'Reset Content', //重置内容
'206' : 'Partial Content', //部分内容
//Redirection 3xx 重定向
'300' : 'Multiple Choices', //多种选择
'301' : 'Moved Permanently', //永久移动,资源(网页等)被永久转移到其它URL
'302' : 'Found', //临时移动,与301类似。但资源只是临时被移动。客户端应继续使用原有URI
'303' : 'See Other', //参见其他
'304' : 'Not Modified', //未修改
'305' : 'Use Proxy', //使用代理
'306' : 'Unused', //未使用
'307' : 'Temporary Redirect', //暂时重定向
//Client Error 4xx 客户端请求错误
'400' : 'Bad Request', //错误的请求
'401' : 'Unauthorized', //未经授权
'402' : 'Payment Required', //付费请求
'403' : 'Forbidden', //禁止
'404' : 'Not Found', //没有找到
'405' : 'Method Not Allowed', //方法不允许
'406' : 'Not Acceptable', //不可接受
'407' : 'Proxy Authentication Required', //需要代理身份验证
'408' : 'Request Timeout', //请求超时
'409' : 'Conflict', //指令冲突
'410' : 'Gone', //文档永久地离开了指定的位置
'411' : 'Length Required', //需要Content-Length头请求
'412' : 'Precondition Failed', //前提条件失败
'413' : 'Request Entity Too Large', //请求实体太大
'414' : 'Request-URI Too Long', //请求URI太长
'415' : 'Unsupported Media Type', //不支持的媒体类型
'416' : 'Requested Range Not Satisfiable', //请求的范围不可满足
'417' : 'Expectation Failed', //期望失败
//Server Error 5xx 服务器错误
'500' : 'Internal Server Error', //内部服务器错误
'501' : 'Not Implemented', //未实现
'502' : 'Bad Gateway', //错误的网关
'503' : 'Service Unavailable', //服务不可用
'504' : 'Gateway Timeout', //网关超时
'505' : 'HTTP Version Not Supported' //HTTP版本不支持
};
2、Java状态码枚举类
package com.hs.common.enums;
public enum HttpStatusCode {
//错误码
public String code;
//英文提示信息
public String enMessage;
//中文提示信息
public String cnMessage;
//获取状态码
@Override
public String getCode() {
return code;
}
//获取英文提示信息
@Override
public String getEnMessage() {
return message;
}
//获取中文提示信息
@Override
public String getCnMessage() {
return message;
}
//构造函数
HttpStatusCode(int code, String enMessage, String zhMessage) {
}
/**
* http状态码枚举所有状态码注解
*/
CONTINUE(100, "Continue", "请继续发送请求的剩余部分"),
SWITCHING_PROTOCOLS(101, "Switching Protocols", "协议切换"),
PROCESSING(102, "Processing", "请求将继续执行"),
CHECKPOINT(103, "Checkpoint", "可以预加载"),
OK(200, "OK", "请求已经成功处理"),
CREATED(201, "Created", "请求已经成功处理,并创建了资源"),
ACCEPTED(202, "Accepted", "请求已经接受,等待执行"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information", "请求已经成功处理,但是信息不是原始的"),
NO_CONTENT(204, "No Content", "请求已经成功处理,没有内容需要返回"),
RESET_CONTENT(205, "Reset Content", "请求已经成功处理,请重置视图"),
PARTIAL_CONTENT(206, "Partial Content", "部分Get请求已经成功处理"),
MULTI_STATUS(207, "Multi-Status", "请求已经成功处理,将返回XML消息体"),
ALREADY_REPORTED(208, "Already Reported", "请求已经成功处理,一个DAV的绑定成员被前一个请求枚举,并且没有被再一次包括"),
IM_USED(226, "IM Used", "请求已经成功处理,将响应一个或者多个实例"),
MULTIPLE_CHOICES(300, "Multiple Choices", "提供可供选择的回馈"),
MOVED_PERMANENTLY(301, "Moved Permanently", "请求的资源已经永久转移"),
FOUND(302, "Found", "请重新发送请求"),
SEE_OTHER(303, "See Other", "请以Get方式请求另一个URI"),
NOT_MODIFIED(304, "Not Modified", "资源未改变"),
USE_PROXY(305, "Use Proxy", "请通过Location域中的代理进行访问"),
TEMPORARY_REDIRECT(307, "Temporary Redirect", "请求的资源临时从不同的URI响应请求"),
RESUME_INCOMPLETE(308, "Resume Incomplete", "请求的资源已经永久转移"),
BAD_REQUEST(400, "Bad Request", "请求错误,请修正请求"),
UNAUTHORIZED(401, "Unauthorized", "没有被授权或者授权已经失效"),
PAYMENT_REQUIRED(402, "Payment Required", "预留状态"),
FORBIDDEN(403, "Forbidden", "请求被理解,但是拒绝执行"),
NOT_FOUND(404, "Not Found", "资源未找到"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed", "请求方法不允许被执行"),
NOT_ACCEPTABLE(406, "Not Acceptable", "请求的资源不满足请求者要求"),
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required", "请通过代理进行身份验证"),
REQUEST_TIMEOUT(408, "Request Timeout", "请求超时"),
CONFLICT(409, "Conflict", "请求冲突"),
GONE(410, "Gone", "请求的资源不可用"),
LENGTH_REQUIRED(411, "Length Required", "Content-Length未定义"),
PRECONDITION_FAILED(412, "Precondition Failed", "不满足请求的先决条件"),
REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large", "请求发送的实体太大"),
REQUEST_URI_TOO_LONG(414, "Request-URI Too Long", "请求的URI超长"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type", "请求发送的实体类型不受支持"),
REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested range not satisfiable", "Range指定的范围与当前资源可用范围不一致"),
EXPECTATION_FAILED(417, "Expectation Failed", "请求头Expect中指定的预期内容无法被服务器满足"),
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity", "请求格式正确,但是由于含有语义错误,无法响应"),
LOCKED(423, "Locked", "当前资源被锁定"),
FAILED_DEPENDENCY(424, "Failed Dependency", "由于之前的请求发生错误,导致当前请求失败"),
UPGRADE_REQUIRED(426, "Upgrade Required", "客户端需要切换到TLS1.0"),
PRECONDITION_REQUIRED(428, "Precondition Required", "请求需要提供前置条件"),
TOO_MANY_REQUESTS(429, "Too Many Requests", "请求过多"),
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large", "请求头超大,拒绝请求"),
INTERNAL_SERVER_ERROR(500, "Internal Server Error", "服务器内部错误"),
NOT_IMPLEMENTED(501, "Not Implemented", "服务器不支持当前请求的部分功能"),
BAD_GATEWAY(502, "Bad Gateway", "响应无效"),
SERVICE_UNAVAILABLE(503, "Service Unavailable", "服务器维护或者过载,拒绝服务"),
GATEWAY_TIMEOUT(504, "Gateway Timeout", "上游服务器超时"),
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version not supported", "不支持的HTTP版本"),
VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates", "服务器内部配置错误"),
INSUFFICIENT_STORAGE(507, "Insufficient Storage", "服务器无法完成存储请求所需的内容"),
LOOP_DETECTED(508, "Loop Detected", "服务器处理请求时发现死循环"),
BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded", "服务器达到带宽限制"),
NOT_EXTENDED(510, "Not Extended", "获取资源所需的策略没有被满足"),
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required", "需要进行网络授权");;
}
参考链接:HTTP状态码—菜鸟教程