@RequestMapping, consumes 提交简单有意思的测试

 

getParm

@GetMapping("getParm")
public Result getParm(String id){
    System.out.println();
    return ResultFactory.success(id);
}

 

等同于 ==

 

 

 

bodyParm

@PostMapping("bodyParm")
public Result bodyParm(@RequestBody String id){
    System.out.println(id);
    return ResultFactory.success(id);
}

 

bodyParm2

@PostMapping(path = "bodyParm2",consumes = MediaType.APPLICATION_JSON_VALUE)
public Result bodyParm2(@RequestBody String id){
    System.out.println(id);
    return ResultFactory.success(id);
}

 

 

总结

1、form-data: 

                   就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。当上传的字段是文件时,会有Content-Type来说明文件类型;content-disposition,用来说明字段的一些信息;

form的enctype属性为编码方式,

常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。 
当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。 
当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 

如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 
但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file)、Content-Type(默认为text/plain)、name(控件name)等信息,并加上分割符(boundary)。

 

1. @PostMapping 未指定提交格式的:  url , form-data,x-www-form-urlencoded 均可以获取到值, 其他方式不行

2. @PostMapping 未指定提交格式时, 但参数使用@RequestBody 设定的, 则必须使用 x-www-form-urlencoded ,或者 raw (json,text,请求体格式),后端接收的数据为整个请求体的内容数据

3. @POSTMapping 未指定提交格式是,但参数使用@NotNull 设定是, 则必须使用url , form-data,x-www-form-urlencoded 均可以获取到值, 其他方式不行

4. @GetMapping 未指定提交格式式, 但参数使用@NotNull 设定时, 则必须使用url  或者 form-data 可以获取到值, 其他方式不行

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章