第一http請(qǐng)求方式
Content-Type 的值類型:
1.1? application/json:消息主體是序列化后的 JSON 字符串。
1.2? application/x-www-form-urlencoded:數(shù)據(jù)被編碼為鍵值對(duì)呢铆。
1.3? multipart/form-data: 需要在表單中進(jìn)行文件上傳時(shí),就需要使用該格式谋币。
1.4? text/plain:數(shù)據(jù)以純文本形式(text/json/xml/html)進(jìn)行編碼,其中不含任何控件或格式字符装获。
第二springmvc注解(常見使用方式)
2.1 類上注解@RestController和@Controller區(qū)別
@RestController注解相當(dāng)于@ResponseBody + @Controller合在一起的作用瑞信。
如果需要返回JSON,用@RestController
如果返回jsp或者h(yuǎn)tml,用@Controller
2.2 方法注解@PostMapping和GetMapping以及@RequestMapping(是@PostMapping和GetMapping的父類)
(PutMapping,DeleteMapping,PatchMapping不常用不做討論)
2.3 方法參數(shù)注解@RequestBody,@ModelAttribute,@RequestParam,@PathVariable,@RequestPart
第三springmvc注解對(duì)應(yīng)的http請(qǐng)求方式
@PostMapping情況下
a @RequestBody ->對(duì)應(yīng)application/json 入?yún)⑹莏avabean
b @ModelAttribute ->對(duì)應(yīng)x-www-form-urlencoded 入?yún)⑹莏avabean ->用戶文件上傳
c @RequestParam -> 對(duì)應(yīng)application/x-www-form-urlencoded 或者?multipart/form-data ?->入?yún)⑹菍?duì)應(yīng)字段 eg:@RequestParam(name = "username", required = true? 沒(méi)傳的話會(huì)報(bào)錯(cuò)404
d?@RequestPart 和?@RequestParam 區(qū)別:
當(dāng)請(qǐng)求頭中指定Content-Type:multipart/form-data時(shí),傳遞的json參數(shù)穴豫,@RequestPart注解可以用對(duì)象來(lái)接收凡简,@RequestParam只能用字符串接收
post請(qǐng)求用ab兩種情況就好,用bean做入?yún)⒑镁S護(hù),好做jsr校驗(yàn).
@GetMapping情況下
a @RequestBody ->入?yún)⑹莏avabean
一般的情況下逼友,GET請(qǐng)求是不可以用@RequestBody來(lái)接收參數(shù)的。 因?yàn)闉g覽器發(fā)起get請(qǐng)求,請(qǐng)求體是空的.
而上層服務(wù)調(diào)用,參數(shù)寫入請(qǐng)求體是可以的.
b @ModelAttribute->入?yún)⑹莏avabean,可做參數(shù)校驗(yàn) 前端拼接url?xxId=xx
c @PathVariable
eg:path="file/down/{fileId}"? @PathVariable(name="fileId", required=true) String fileId
前端拼接url= xxx/file/download/fileId