第一http请求方式
Content-Type 的值类型:
1.1 application/json:消息主体是序列化后的 JSON 字符串。
1.2 application/x-www-form-urlencoded:数据被编码为键值对。
1.3 multipart/form-data: 需要在表单中进行文件上传时,就需要使用该格式。
1.4 text/plain:数据以纯文本形式(text/json/xml/html)进行编码,其中不含任何控件或格式字符。
第二springmvc注解(常见使用方式)
2.1 类上注解@RestController和@Controller区别
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
如果需要返回JSON,用@RestController
如果返回jsp或者html,用@Controller
2.2 方法注解@PostMapping和GetMapping以及@RequestMapping(是@PostMapping和GetMapping的父类)
(PutMapping,DeleteMapping,PatchMapping不常用不做讨论)
2.3 方法参数注解@RequestBody,@ModelAttribute,@RequestParam,@PathVariable
第三springmvc注解对应的http请求方式
@PostMapping情况下
a @RequestBody ->对应application/json 入参是javabean
b @ModelAttribute ->对应x-www-form-urlencoded 入参是javabean ->用户文件上传
c @RequestParam -> 对应application/x-www-form-urlencoded ->入参是对应字段 eg:@RequestParam(name = "username", required = true 没传的话会报错404
post请求用ab两种情况就好,用bean做入参好维护,好做jsr校验.
@GetMapping情况下
a @RequestBody ->入参是javabean
一般的情况下,GET请求是不可以用@RequestBody来接收参数的。 因为浏览器发起get请求,请求体是空的.
而上层服务调用,参数写入请求体是可以的.
b @ModelAttribute->入参是javabean,可做参数校验 前端拼接url?xxId=xx
c @PathVariable
eg:path="file/down/{fileId}" @PathVariable(name="fileId", required=true) String fileId
前端拼接url= xxx/file/download/fileId
网友评论