天天看點

Http請求中Content-Type講解以及在Spring MVC中的應用

引言: 在Http請求中,我們每天都在使用Content-type來指定不同格式的請求資訊,但是卻很少有人去全面了解content-type中允許的值有多少,這裡将講解Content-Type的可用值,以及在Spring MVC中如何使用它們來映射請求資訊。

Content-Type

MediaType,即是Internet Media Type,網際網路媒體類型;也叫做MIME類型,在Http協定消息頭中,使用Content-Type來表示具體請求中的媒體類型資訊。

[html] view plain copy

類型格式:type/subtype(;parameter)? type

主類型,任意的字元串,如text,如果是*号代表所有;

subtype 子類型,任意的字元串,如html,如果是*号代表所有;

parameter 可選,一些參數,如Accept請求頭的q參數, Content-Type的 charset參數。

例如: Content-Type: text/html;charset:utf-8;

常見的媒體格式類型如下:

以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 :

中預設的encType,form表單資料被編碼為key/value格式發送到伺服器(表單預設的送出資料的格式)

另外一種常見的媒體格式是上傳檔案之時使用的:

Spring MVC中關于關于Content-Type類型資訊的使用

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Mapping

public @interface RequestMapping {

}

value: 指定請求的實際位址, 比如 /action/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-typ資訊進行過濾資訊;headers中可以使用content-type進行過濾和判斷。

使用示例

3.1 headers

public void testHeaders(@PathVariable String ownerId, @PathVariable String petId) {

// implementation omitted

這裡的Headers裡面可以比對所有Header裡面可以出現的資訊,不局限在Referer資訊。

示例2

@RequestMapping(value = "/response/ContentType", headers = "Accept=application/json")

public void response2(HttpServletResponse response) throws IOException {

伺服器根據請求頭“Accept=application/json”生産json資料。

當你有如下Accept頭,将遵守如下規則進行應用:

①Accept:text/html,application/xml,application/json

②Accept:application/xml;q=0.5,application/json;q=0.9,text/html

③Accept:/,text/*,text/html

即比對規則為:最明确的優先比對。

Requests部分

Header 解釋 示例

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 QWxhZGRpbjpvcGVuIHNlc2FtZQ==

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 2010 08:12:31 GMT

Expect 請求的特定的伺服器行為 Expect: 100-continue

From 送出請求的使用者的Email From: [email protected]

Host 指定請求的伺服器的域名和端口号 Host: www.zcmhi.com

If-Match 隻有請求内容與實體相比對才有效 If-Match: “737060cd8c284d8af7ad3082f209582d”

If-Modified-Since 如果請求的部分在指定時間之後被修改則請求成功,未被修改則傳回304代碼 If-Modified-Since: Sat, 29 Oct 2010 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 2010 19:43:31 GMT

Max-Forwards 限制資訊通過代理和網關傳送的時間 Max-Forwards: 10

Pragma 用來包含實作特定的指令 Pragma: no-cache

Proxy-Authorization 連接配接到代理的授權證書 Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Range 隻請求實體的一部分,指定範圍 Range: bytes=500-999

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

Responses 部分

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 2010 08:12:31 GMT

ETag 請求變量的實體标簽的目前值 ETag: “737060cd8c284d8af7ad3082f209582d”

Expires 響應過期的日期和時間 Expires: Thu, 01 Dec 2010 16:00:00 GMT

Last-Modified 請求資源的最後修改時間 Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT

Pragma 包括實作特定的指令,它可應用到響應鍊上的任何接收方 Pragma: no-cache

Proxy-Authenticate 它指出認證方案和可應用到代理的該URL上的參數 Proxy-Authenticate: Basic

refresh 應用于重定向或一個新的資源被創造,在5秒之後重定向(由網景提出,被大部分浏覽器支援)

Refresh: 5; url=

<a href="http://www.zcmhi.com/archives/94.html">http://www.zcmhi.com/archives/94.html</a>

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

3.2 params的示例

@RequestMapping(value = "/test/{userId}", method = RequestMethod.GET, params="myParam=myValue")

public void findUser(@PathVariable String userId) {

僅處理請求中包含了名為“myParam”,值為“myValue”的請求,起到了一個過濾的作用。

3.3 consumes/produces

@Controller

@RequestMapping(value = "/users", method = RequestMethod.POST, consumes="application/json", produces="application/json")

@ResponseBody

public List addUser(@RequestBody User userl) {

方法僅處理request Content-Type為“application/json”類型的請求. produces辨別==&gt;處理request請求中Accept頭中包含了"application/json"的請求,同時暗示了傳回的内容類型為application/json;

總結

在本文中,首先介紹了Content-Type主要支援的格式内容,然後基于@RequestMapping标注的内容介紹了主要的使用方法,其中,headers, consumes,produces,都是使用Content-Type中使用的各種媒體格式内容,可以基于這個格式内容來進行通路的控制和過濾。

參考資料: