http://blog.csdn.net/blueheart20/article/details/45174399
引言: 在Http請(qǐng)求中,我們每天都在使用Content-type來指定不同格式的請(qǐng)求信息,但是卻很少有人去全面了解content-type中允許的值有多少,這里將講解Content-Type的可用值,以及在spring MVC中如何使用它們來映射請(qǐng)求信息。
- Content-Type
MediaType,即是Internet Media Type,互聯(lián)網(wǎng)媒體類型;也叫做MIME類型,在Http協(xié)議消息頭中,使用Content-Type來表示具體請(qǐng)求中的媒體類型信息。
[html] view plain copy
在CODE上查看代碼片派生到我的代碼片
類型格式:type/subtype(;parameter)? type
主類型,任意的字符串,如text,如果是號(hào)代表所有;
subtype 子類型,任意的字符串,如html,如果是號(hào)代表所有;
parameter 可選,一些參數(shù),如Accept請(qǐng)求頭的q參數(shù), Content-Type的 charset參數(shù)。
例如: Content-Type: text/html;charset:utf-8;
常見的媒體格式類型如下:
text/html : HTML格式
text/plain :純文本格式
text/xml : XML格式
image/gif :gif圖片格式
image/jpeg :jpg圖片格式
image/png:png圖片格式
以application開頭的媒體格式類型:
application/xhtml+xml :XHTML格式
application/xml : XML數(shù)據(jù)格式
application/atom+xml :Atom XML聚合格式
application/json : JSON數(shù)據(jù)格式
application/pdf :pdf格式
application/msword : Word文檔格式
application/octet-stream : 二進(jìn)制流數(shù)據(jù)(如常見的文件下載)
application/x-www-form-urlencoded : <form encType=””>中默認(rèn)的encType,form表單數(shù)據(jù)被編碼為key/value格式發(fā)送到服務(wù)器(表單默認(rèn)的提交數(shù)據(jù)的格式)
另外一種常見的媒體格式是上傳文件之時(shí)使用的:
multipart/form-data : 需要在表單中進(jìn)行文件上傳時(shí),就需要使用該格式
以上就是我們?cè)谌粘5拈_發(fā)中,經(jīng)常會(huì)用到的若干content-type的內(nèi)容格式。
- Spring MVC中關(guān)于關(guān)于Content-Type類型信息的使用
首先我們來看看RequestMapping中的Class定義:
[html] view plain copy
在CODE上查看代碼片派生到我的代碼片
@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: 指定請(qǐng)求的實(shí)際地址, 比如 /action/info之類。method: 指定請(qǐng)求的method類型, GET、POST、PUT、DELETE等consumes: 指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type),例如application/json, text/html;produces: 指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)類型中包含該指定類型才返回params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請(qǐng)求
其中,consumes, produces使用content-typ信息進(jìn)行過濾信息;headers中可以使用content-type進(jìn)行過濾和判斷。
- 使用示例
3.1 headers
[html] view plain copy
在CODE上查看代碼片派生到我的代碼片
@RequestMapping(value = "/test", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
public void testHeaders(@PathVariable String ownerId, @PathVariable String petId) {
// implementation omitted
}
這里的Headers里面可以匹配所有Header里面可以出現(xiàn)的信息,不局限在Referer信息。
示例2
[html] view plain copy
@RequestMapping(value = "/response/ContentType", headers = "Accept=application/json")
public void response2(HttpServletResponse response) throws IOException {
//表示響應(yīng)的內(nèi)容區(qū)數(shù)據(jù)的媒體類型為json格式,且編碼為utf-8(客戶端應(yīng)該以u(píng)tf-8解碼)
response.setContentType("application/json;charset=utf-8");
//寫出響應(yīng)體內(nèi)容
String jsonData = "{"username":"zhang", "password":"123"}";
response.getWriter().write(jsonData);
}
服務(wù)器根據(jù)請(qǐng)求頭“Accept=application/json”生產(chǎn)json數(shù)據(jù)。
當(dāng)你有如下Accept頭,將遵守如下規(guī)則進(jìn)行應(yīng)用:①Accept:text/html,application/xml,application/json 將按照如下順序進(jìn)行produces的匹配 ①text/html ②application/xml ③application/json②Accept:application/xml;q=0.5,application/json;q=0.9,text/html 將按照如下順序進(jìn)行produces的匹配 ①text/html ②application/json ③application/xml 參數(shù)為媒體類型的質(zhì)量因子,越大則優(yōu)先權(quán)越高(從0到1)③Accept:/,text/,text/html 將按照如下順序進(jìn)行produces的匹配 ①text/html ②text/ ③/
即匹配規(guī)則為:最明確的優(yōu)先匹配。
Requests部分
Header
解釋
示例
Accept
指定客戶端能夠接收的內(nèi)容類型
Accept: text/plain, text/html
Accept-Charset
瀏覽器可以接受的字符編碼集。
Accept-Charset: iso-8859-5
Accept-Encoding
指定瀏覽器可以支持的web服務(wù)器返回內(nèi)容壓縮編碼類型。
Accept-Encoding: compress, gzip
Accept-Language
瀏覽器可接受的語言
Accept-Language: en,zh
Accept-Ranges
可以請(qǐng)求網(wǎng)頁(yè)實(shí)體的一個(gè)或者多個(gè)子范圍字段
Accept-Ranges: bytes
Authorization
HTTP授權(quán)的授權(quán)證書
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control
指定請(qǐng)求和響應(yīng)遵循的緩存機(jī)制
Cache-Control: no-cache
Connection
表示是否需要持久連接。(HTTP 1.1默認(rèn)進(jìn)行持久連接)
Connection: close
Cookie
HTTP請(qǐng)求發(fā)送時(shí),會(huì)把保存在該請(qǐng)求域名下的所有cookie值一起發(fā)送給web服務(wù)器。
Cookie: $Version=1; Skin=new;
Content-Length
請(qǐng)求的內(nèi)容長(zhǎng)度
Content-Length: 348
Content-Type
請(qǐng)求的與實(shí)體對(duì)應(yīng)的MIME信息
Content-Type: application/x-www-form-urlencoded
Date
請(qǐng)求發(fā)送的日期和時(shí)間
Date: Tue, 15 Nov 2010 08:12:31 GMT
Expect
請(qǐng)求的特定的服務(wù)器行為
Expect: 100-continue
From
發(fā)出請(qǐng)求的用戶的Email
From: user@email.com
Host
指定請(qǐng)求的服務(wù)器的域名和端口號(hào)
Host: www.zcmhi.com
If-Match
只有請(qǐng)求內(nèi)容與實(shí)體相匹配才有效
If-Match: “737060cd8c284d8af7ad3082f209582d”
If-Modified-Since
如果請(qǐng)求的部分在指定時(shí)間之后被修改則請(qǐng)求成功,未被修改則返回304代碼
If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT
If-None-Match
如果內(nèi)容未改變返回304代碼,參數(shù)為服務(wù)器先前發(fā)送的Etag,與服務(wù)器回應(yīng)的Etag比較判斷是否改變
If-None-Match: “737060cd8c284d8af7ad3082f209582d”
If-Range
如果實(shí)體未改變,服務(wù)器發(fā)送客戶端丟失的部分,否則發(fā)送整個(gè)實(shí)體。參數(shù)也為Etag
If-Range: “737060cd8c284d8af7ad3082f209582d”
If-Unmodified-Since
只在實(shí)體在指定時(shí)間之后未被修改才請(qǐng)求成功
If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT
Max-Forwards
限制信息通過代理和網(wǎng)關(guān)傳送的時(shí)間
Max-Forwards: 10
Pragma
用來包含實(shí)現(xiàn)特定的指令
Pragma: no-cache
Proxy-Authorization
連接到代理的授權(quán)證書
Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Range
只請(qǐng)求實(shí)體的一部分,指定范圍
Range: bytes=500-999
Referer
先前網(wǎng)頁(yè)的地址,當(dāng)前請(qǐng)求網(wǎng)頁(yè)緊隨其后,即來路
Referer: http://www.zcmhi.com/archives/71.html
TE
客戶端愿意接受的傳輸編碼,并通知服務(wù)器接受接受尾加頭信息
TE: trailers,deflate;q=0.5
Upgrade
向服務(wù)器指定某種傳輸協(xié)議以便服務(wù)器進(jìn)行轉(zhuǎn)換(如果支持)
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
User-Agent
User-Agent的內(nèi)容包含發(fā)出請(qǐng)求的用戶信息
User-Agent: Mozilla/5.0 (Linux; X11)
Via
通知中間網(wǎng)關(guān)或代理服務(wù)器地址,通信協(xié)議
Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning
關(guān)于消息實(shí)體的警告信息
Warn: 199 Miscellaneous warning
Responses 部分
Header
解釋
示例
Accept-Ranges
表明服務(wù)器是否支持指定范圍請(qǐng)求及哪種類型的分段請(qǐng)求
Accept-Ranges: bytes
Age
從原始服務(wù)器到代理緩存形成的估算時(shí)間(以秒計(jì),非負(fù))
Age: 12
Allow
對(duì)某網(wǎng)絡(luò)資源的有效的請(qǐng)求行為,不允許則返回405
Allow: GET, HEAD
Cache-Control
告訴所有的緩存機(jī)制是否可以緩存及哪種類型
Cache-Control: no-cache
Content-Encoding
web服務(wù)器支持的返回內(nèi)容壓縮編碼類型。
Content-Encoding: gzip
Content-Language
響應(yīng)體的語言
Content-Language: en,zh
Content-Length
響應(yīng)體的長(zhǎng)度
Content-Length: 348
Content-Location
請(qǐng)求資源可替代的備用的另一地址
Content-Location: /index.htm
Content-MD5
返回資源的MD5校驗(yàn)值
Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range
在整個(gè)返回體中本部分的字節(jié)位置
Content-Range: bytes 21010-47021/47022
Content-Type
返回內(nèi)容的MIME類型
Content-Type: text/html; charset=utf-8
Date
原始服務(wù)器消息發(fā)出的時(shí)間
Date: Tue, 15 Nov 2010 08:12:31 GMT
ETag
請(qǐng)求變量的實(shí)體標(biāo)簽的當(dāng)前值
ETag: “737060cd8c284d8af7ad3082f209582d”
Expires
響應(yīng)過期的日期和時(shí)間
Expires: Thu, 01 Dec 2010 16:00:00 GMT
Last-Modified
請(qǐng)求資源的最后修改時(shí)間
Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT
Location
用來重定向接收方到非請(qǐng)求URL的位置來完成請(qǐng)求或標(biāo)識(shí)新的資源
Location: http://www.zcmhi.com/archives/94.html
Pragma
包括實(shí)現(xiàn)特定的指令,它可應(yīng)用到響應(yīng)鏈上的任何接收方
Pragma: no-cache
Proxy-Authenticate
它指出認(rèn)證方案和可應(yīng)用到代理的該URL上的參數(shù)
Proxy-Authenticate: Basic
refresh
應(yīng)用于重定向或一個(gè)新的資源被創(chuàng)造,在5秒之后重定向(由網(wǎng)景提出,被大部分瀏覽器支持)
Refresh: 5; url=
http://www.zcmhi.com/archives/94.html
Retry-After
如果實(shí)體暫時(shí)不可取,通知客戶端在指定時(shí)間之后再次嘗試
Retry-After: 120
Server
web服務(wù)器軟件名稱
Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
Set-Cookie
設(shè)置Http Cookie
Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
Trailer
指出頭域在分塊傳輸編碼的尾部存在
Trailer: Max-Forwards
Transfer-Encoding
文件傳輸編碼
Transfer-Encoding:chunked
Vary
告訴下游代理是使用緩存響應(yīng)還是從原始服務(wù)器請(qǐng)求
Vary: *
Via
告知代理客戶端響應(yīng)是通過哪里發(fā)送的
Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning
警告實(shí)體可能存在的問題
Warning: 199 Miscellaneous warning
WWW-Authenticate
表明客戶端請(qǐng)求實(shí)體應(yīng)該使用的授權(quán)方案
WWW-Authenticate: Basic
3.2 params的示例
[html] view plain copy
@RequestMapping(value = "/test/{userId}", method = RequestMethod.GET, params="myParam=myValue")
public void findUser(@PathVariable String userId) {
// implementation omitted
}
僅處理請(qǐng)求中包含了名為“myParam”,值為“myValue”的請(qǐng)求,起到了一個(gè)過濾的作用。
3.3 consumes/produces
[html] view plain copy
@Controller
@RequestMapping(value = "/users", method = RequestMethod.POST, consumes="application/json", produces="application/json")
@ResponseBody
public List<User> addUser(@RequestBody User userl) {
// implementation omitted
return List<User> users;
}
方法僅處理request Content-Type為“application/json”類型的請(qǐng)求. produces標(biāo)識(shí)==>處理request請(qǐng)求中Accept頭中包含了"application/json"的請(qǐng)求,同時(shí)暗示了返回的內(nèi)容類型為application/json;
- 總結(jié)
在本文中,首先介紹了Content-Type主要支持的格式內(nèi)容,然后基于@RequestMapping標(biāo)注的內(nèi)容介紹了主要的使用方法,其中,headers, consumes,produces,都是使用Content-Type中使用的各種媒體格式內(nèi)容,可以基于這個(gè)格式內(nèi)容來進(jìn)行訪問的控制和過濾。
參考資料: - HTTP中支持的Content-Type: http://tool.oschina.NET/commons
- Media Type介紹。 http://www.iteye.com/topic/1127120