欢迎各位兄弟 发布技术文章
这里的技术是共享的
x-www-form-urlencoded
纸面翻译即所谓url格式的编码,是post的默认Content-Type,其实我觉得可以认为get和post的默认表单数据传递格式都一样,只是一个在url地址后面加 ?再加表单数据,另一个是把表单数据写在请求体內
请求头內的Content-Type
字段里,
Content-Type:application/x-www-form-urlencoded
get请求的请求体格式是什么?get请求是拼接在url后面请求的,一般如此username=tom&pwd=123
,这样的格式叫查询参数,x-www-form-urlencoded
也长这样,只是不添加到url后面;
要知道post的默认数据传输格式就是x-www-form-urlencoded
,所以为什么在post数据的时候需要把数据转为url格式(username=tom&pwd=123
),一般使用qs
库的qs.stringify()
方法就能把json对象转换成url格式编码(x-www-form-urlencoded
)
来自 https://blog.csdn.net/qq_29923881/article/details/103500948
通常在Springboot接收参数时候,都是json格式的传输,使用restful风格,而在实际开发中,可能会接收其它格式的参数,比如x-www-form-urlencoded格式的数据
就是application/x-www-from-urlencoded,会将表单内的数据转换为键值对,比如,name=zhangsan&age = 18
代码如下(示例):
@PostMapping("/efly/get") @ResponseBody public RespEntity getEFlyReturnPR(@RequestBody String cd) { System.out.println("接收到的x-www-form-urlencoded"); System.out.println(cd); return respEntity=new RespEntity(RespCode.CODE_200,cd); }
代码如下(示例):
@PostMapping("/efly/get") @ResponseBody public RespEntity getEFlyReturnPR(@RequestParam Map<String, String> params) { System.out.println("接收到的x-www-form-urlencoded"); System.out.println(params); return respEntity=new RespEntity(RespCode.CODE_200,params); } }
代码如下(示例):
@PostMapping("/efly/get") @ResponseBody public RespEntity getEFlyReturnPR(@RequestParam LinkedHashMap<String, String> params) { System.out.println("接收到的x-www-form-urlencoded"); System.out.println(params); return respEntity=new RespEntity(RespCode.CODE_200,params); } }
postman:
控制台输出:
使用map数据类型:
postman:
自己封装的实体:
后端代码:
postman报错:
当传入参数是x-www-form-urlencoded,接收参数@RequestBody+ String/Map/LinkedHashMap即可接收成功
@PostMapping("/efly/get") @ResponseBody public RespEntity getEFlyReturnPR(@RequestBody String cd) { System.out.println("接收到的x-www-form-urlencoded"); System.out.println(cd); return respEntity=new RespEntity(RespCode.CODE_200,cd); } }
@PostMapping("/efly/get") @ResponseBody public RespEntity getEFlyReturnPR(@RequestBody Map<String, String> params) { System.out.println("接收到的x-www-form-urlencoded"); System.out.println(params); return respEntity=new RespEntity(RespCode.CODE_200,cd); }
@PostMapping("/efly/get")
@ResponseBody
public RespEntity getEFlyReturnPR(@RequestParam LinkedHashMap<String, String> params) {
System.out.println("接收到的x-www-form-urlencoded");
System.out.println(params);
return respEntity=new RespEntity(RespCode.CODE_200,params);
}
}
来自 https://blog.csdn.net/weixin_51614089/article/details/117067441