http請求頭中的content-type屬性
在HTTP請求中,我們每天都在使用Content-Type來指定不同格式的請求資訊,但是卻很少有人去全面瞭解Content-Type中允許的值有多少,因此這裡來了解一下Content-Type的可用值,以及在Spring MVC中如何使用它們來對映請求資訊。
什麼是Content-Type
要知道什麼是Content-Type,首先要了解什麼是Internet Media Type。Internet Media Type即網際網路媒體型別,也叫做MIME型別,使用兩部分識別符號來確定一個型別。在HTTP協議訊息頭中,使用Content-Type來表示具體請求中的媒體型別資訊,意思就是說,Content-Type是Internet Media Type在HTTP協議中的別稱。
Content-Type的格式
type/subtype(;parameter)? type
上面是Content-Type的格式,可以拆解為三個部分,分別是主型別(type)、子型別(subtype)和引數(parameter)。
主型別(type)
主型別可以是任意的字串,比如text。如果是*號則代表所有型別。
子型別(subtype)
子型別可以是任意的字串,比如html。如果是*號則代表所有型別。
引數(parameter)
引數是可選的,可以在Content-Type中加入一些特殊的引數,比如Accept請求頭的引數,常見的有用於設定字元編碼的charset引數。
Content-Type: text/html;charset:utf-8;
Content-Type中常見的媒體格式型別
以text開頭的媒體格式型別:
text/html: HTML格式。
text/plain:純文字格式。
text/xml: XML格式。
以image開頭的媒體格式型別:
image/gif:gif圖片格式。
image/jpeg:jpg圖片格式。
image/png:png圖片格式。
以application開頭的媒體格式型別:
application/xhtml+xml:XHTML格式。
application/xml: XML資料格式。
application/atom+xml:Atom XML聚合格式 。
application/json: JSON資料格式。
application/pdf:pdf格式 。
application/msword: Word文件格式。
application/octet-stream: 二進位制流資料(如常見的檔案下載)。
application/x-www-form-urlencoded: <form encType=””>中預設的encType,form表單資料被編碼為key/value格式傳送到伺服器(表單預設的提交資料的格式)。
另外還有一種常見的媒體格式是上傳檔案之時使用的:
multipart/form-data : 需要在表單中進行檔案上傳時,就需要使用該格式。
以上就是我們在日常的開發中,經常會用到的若干Content-Type的內容格式。
Spring MVC中關於Content-Type型別資訊的使用
在Spring MVC中,主要就是使用@RequestMapping註解來處理請求,因此首先我們來看看@RequestMapping註解的Class定義。
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String[] value() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; }
下面對註解提供的引數進行簡單解析。
value:指定請求的實際地址, 比如/yanggb/info之類的請求路由。
method: 指定請求的method型別, 比如GET、POST、PUT、DELETE等。
consumes:指定處理請求的提交內容型別(Content-Type),例如application/json、text/html;等。
produces:指定返回的內容型別,僅當request請求頭中的(Accept)型別中包含該指定型別才返回。
params:指定request中必須包含某些引數值,才讓該方法處理。
headers:指定request中必須包含某些指定的header值,才能讓該方法處理請求。
其中,consumes、produces可以使用Content-type中的資訊對不需要的資訊進行過濾,僅接受需要的資料;headers則可以使用Content-Type中的資訊對請求進行過濾和判斷。
Request Headers和Response Headers中的候選屬性
這裡列舉出Request Headers(請求頭)和Response Headers(響應頭)中的候選屬性。
Request Headers的候選屬性
候選屬性 | 說明 | 示例 |
---|---|---|
Accept | 指定客戶端能夠接收的內容型別 | Accept: text/plain, text/html |
Accept-Charset | 瀏覽器可以接受的字元編碼集 | Accept-Charset: iso-8859-5 |
Accept-Encoding | 指定瀏覽器可以支援的web伺服器返回內容壓縮編碼型別 | Accept-Encoding: compress, gzip |
Accept-Language | 瀏覽器可接受的語言 | Accept-Language: en,zh |
Accept-Ranges | 可以請求網頁實體的一個或者多個子範圍欄位 | Accept-Ranges: bytes |
Authorization | HTTP授權的授權證書 | Authorization: Basic QWxhZLRpbjpvcGVuIHNoc2FtZQ== |
Cache-Control | 指定請求和響應遵循的快取機制 | Cache-Control: no-cache |
Connection | 表示是否需要持久連線。(HTTP 1.1預設進行持久連線) | Connection: close |
Cookie | HTTP請求傳送時,會把儲存在該請求域名下的所有cookie值一起傳送給web伺服器 | Cookie: $Version=1; Skin=new; |
Content-Length | 請求的內容長度 | Content-Length: 348 |
Content-Type | 請求的與實體對應的MIME資訊 | Content-Type: application/x-www-form-urlencoded |
Date | 請求傳送的日期和時間 | Date: Tue, 15 Nov 2018 08:22:31 GMT |
Expect | 請求的特定的伺服器行為 | Expect: 100-continue |
From | 發出請求的使用者的Email | From: [email protected] |
Host | 指定請求的伺服器的域名和埠號 | Host: www.yanggb.com |
If-Match | 只有請求內容與實體相匹配才有效 | If-Match: "737060ff8c284d8af7ad2082f209582d" |
If-Modified-Since | 如果請求的部分在指定時間之後被修改則請求成功,未被修改則返回304程式碼 | If-Modified-Since: Sat, 29 Oct 2018 19:43:31 GMT |
If-None-Match | 如果內容未改變返回304程式碼,引數為伺服器先前傳送的Etag,與伺服器迴應的Etag比較判斷是否改變 | If-None-Match: "737060cd8c284d8af7ad3082f209582d" |
If-Range | 如果實體未改變,伺服器傳送客戶端丟失的部分,否則傳送整個實體。引數也為Etag | If-Range: "737060cd8c284d8af7ad3082f209582d" |
If-Unmodified-Since | 只在實體在指定時間之後未被修改才請求成功 | If-Unmodified-Since: Sat, 29 Oct 2018 19:23:11 GMT |
Max-Forwards | 限制資訊通過代理和閘道器傳送的時間 | Max-Forwards: 10 |
Pragma | 用來包含實現特定的指令 | Pragma: no-cache |
Proxy-Authorization | 連線到代理的授權證書 | Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
Range | 只請求實體的一部分,指定範圍 | Range: bytes=500-999 |
Referer | 先前網頁的地址,當前請求網頁緊隨其後,即來路 | Referer: http://www.yanggb.com/yanggb1.html |
TE | 客戶端願意接受的傳輸編碼,並通知伺服器接受接受尾加頭資訊 | TE: trailers,deflate;q=0.5 |
Upgrade | 向伺服器指定某種傳輸協議以便伺服器進行轉換(如果支援) | Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 |
User-Agent | User-Agent的內容包含發出請求的使用者資訊 | User-Agent: Mozilla/5.0 (Linux; X11) |
Via | 通知中間閘道器或代理伺服器地址,通訊協議 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning | 關於訊息實體的警告資訊 | Warn: 199 Miscellaneous warning |
可以看到,Content-Type是Request Headers中的一個候選屬性。
Response Headers的候選屬性
候選屬性 | 說明 | 示例 |
---|---|---|
Accept-Ranges | 表明伺服器是否支援指定範圍請求及哪種型別的分段請求 | Accept-Ranges: bytes |
Age | 從原始伺服器到代理快取形成的估算時間(以秒計,非負) | Age: 12 |
Allow | 對某網路資源的有效的請求行為,不允許則返回405 | Allow: GET, HEAD |
Cache-Control | 告訴所有的快取機制是否可以快取及哪種型別 | Cache-Control: no-cache |
Content-Encoding | web伺服器支援的返回內容壓縮編碼型別 | Content-Encoding: gzip |
Content-Language | 響應體的語言 | Content-Language: en,zh |
Content-Length | 響應體的長度 | Content-Length: 348 |
Content-Location | 請求資源可替代的備用的另一地址 | Content-Location: /index.htm |
Content-MD5 | 返回資源的MD5校驗值 | Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== |
Content-Range | 在整個返回體中本部分的位元組位置 | Content-Range: bytes 21010-47021/47022 |
Content-Type | 返回內容的MIME型別 | Content-Type: text/html; charset=utf-8 |
Date | 原始伺服器訊息發出的時間 | Date: Tue, 15 Nov 2018 08:22:22 GMT |
ETag | 請求變數的實體標籤的當前值 | ETag: "737060cd8c284d8af7ad3082f209582d" |
Expires | 響應過期的日期和時間 | Expires: Thu, 01 Dec 2018 16:00:00 GMT |
Last-Modified | 請求資源的最後修改時間 | Last-Modified: Tue, 15 Nov 2018 12:25:26 GMT |
Location | 用來重定向接收方到非請求URL的位置來完成請求或標識新的資源 | Location: http://www.yanggb.com/yanggb2.html |
Pragma | 包括實現特定的指令,它可應用到響應鏈上的任何接收方 | Pragma: no-cache |
Proxy-Authenticate | 它指出認證方案和可應用到代理的該URL上的引數 | Proxy-Authenticate: Basic |
refresh | 應用於重定向或一個新的資源被創造,在5秒之後重定向(由網景提出,被大部分瀏覽器支援) | Refresh: 5; url= http://www.yanggb.com/yanggb9.html |
Retry-After | 如果實體暫時不可取,通知客戶端在指定時間之後再次嘗試 | Retry-After: 120 |
Server | web伺服器軟體名稱 | Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) |
Set-Cookie | 設定Http Cookie | Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1 |
Trailer | 指出頭域在分塊傳輸編碼的尾部存在 | Trailer: Max-Forwards |
Transfer-Encoding | 檔案傳輸編碼 | Transfer-Encoding:chunked |
Vary | 告訴下游代理是使用快取響應還是從原始伺服器請求 | Vary: * |
Via | 告知代理客戶端響應是通過哪裡傳送的 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning | 警告實體可能存在的問題 | Warning: 199 Miscellaneous warning |
WWW-Authenticate | 表明客戶端請求實體應該使用的授權方案 | WWW-Authenticate: Basic |
具體使用示例
這裡通過幾個具體的例項來了解怎麼在Spring MVC中使用兩個Headers中的資訊。
示例1:headers屬性取Request Headers中的Referer屬性。
@RequestMapping(value = "/yanggb2", method = RequestMethod.GET, headers="Referer=http://www.yanggb.com/yanggb1") public void yanggb2() { // 如果上一個頁面的地址不是上面headers屬性中Referer指定的地址,不會執行此方法 }
這裡的headers裡面可以匹配所有Headers裡面可以出現的資訊,不侷限於Referer資訊。
示例2:headers屬性取Request Headers中的Accept屬性。
@RequestMapping(value = "/yanggb2", headers = "Accept=application/json") public void yanggb3(HttpServletResponse response) throws IOException { // 表示響應的內容區資料的媒體型別為json格式,且編碼為utf-8(客戶端應該以utf-8解碼) response.setContentType("application/json;charset=utf-8"); // 寫出響應體內容 String jsonData = "{\"username\":\"yanggb\",\"password\":\"123\"}"; response.getWriter().write(jsonData); }
這樣,伺服器就會根據請求頭【Accept=application/json】生產json資料。
當你有如下Accept頭,將遵守如下規則進行應用:
①Accept:text/html,application/xml,application/json
將按照如下順序進行produces的匹配 ①text/html ②application/xml ③application/json。
②Accept:application/xml;q=0.5,application/json;q=0.9,text/html。
將按照如下順序進行produces的匹配 ①text/html ②application/json ③application/xml。
引數為媒體型別的質量因子,越大則優先權越高(從0到1)。
③Accept:*/*,text/*,text/html
將按照如下順序進行produces的匹配 ①text/html ②text/* ③*/*。
即匹配規則為:最明確的優先匹配。
示例3:params屬性取引數。
@RequestMapping(value = "/yanggb/{userId}", method = RequestMethod.GET, params = "yanggb1=yanggb2") public void findUser(@PathVariable String userId) { // 當請求中包含引數yanggb1時才會執行此方法 }
在上面的程式碼中,就起到了一個過濾的作用。
例項4:consumes/produces屬性過濾請求。
@Controller @RequestMapping(value = "/users", method = RequestMethod.POST, consumes="application/json", produces = "application/json") @ResponseBody public List<User> addUser(@RequestBody User userl) { return List<User> users; }
上面這個方法僅處理請求Content-Type為【application/json】型別的請求,produces標識處理請求中Accept頭中包含了【application/json】的請求,同時暗示了返回的內容型別為【application/json】。
"你是有多不會表達喜歡,才會一直說不知道