后端如何接收格式为x-www-form-urlencoded的数据
1.x-www-form-urlencoded是什么?
x-www-form-urlencoded纸面翻译即所谓url格式的编码,是post的默认Content-Type,其实就是一种编码格式,类似json也是一种编码传输格式。form表单中使用
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
2.x-www-form-urlencoded类型后端怎么接收
用登录案例来作说明,登录的时候需要输入账户跟密码。但是前端使用的是x-www-form-urlencoded类型传输,所以我们也需要使用x-www-form-urlencoded类型接收

2.1后端Controller层接收代码一
@ApiOperation(value = "用户登陆获取token", position = 5, notes = "用户登陆获取token")
@ApiImplicitParams({
@ApiImplicitParam(name = "account", value = "用户名", dataType = "String"),
@ApiImplicitParam(name = "password", value = "密码", dataType = "String"),
})
@RequestMapping(value="/login",method= RequestMethod.POST)
public Result loginPC(String account ,String password ) throws Exception {
Map map = UserService.LoginUer(account, password);
return Result.success(map);
}需要使用到@ApiImplicitParam,若有多个参数的话使用@ApiImplicition进行包裹。
接收x-www-form-urlencoded类型的关键点就在于@ApiImplicitParam。免去了使用@RequestBody在写一个接收类的繁琐步骤,加上@ApiImplicitParam之后直接接收即可。
若使用application/x-www-form-urlencoded类型传输数据过来,后端使用@ReposeBody接收,或出现报错
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
若出现上边的错误,只需要按照上边代码修改一些接收参数即可
所以使用application/x-www-form-urlencoded类型传输数据过来,需要使用@ApiImplicitParam说明参数并且接收
2.2@ApiImplicitParam说明
@ApiImplicitParam是一个标注方法参数的注解
注解内的属性有
name:参数名
value:参数的中文含义
required:是否必须
dataType:参数类型
paramType:参数所放位置
defaultValue:参数的默认值
其中,paramType可选值有header、query、path
header标注为从@RequestHeader中获取
query标注为从@RequestParam中获取
path从标注为@PathVariable中获取
方法中有多个参数时,使用@ApiImplicitParams包围
@ApiImplicitParams({
@ApiImplicitParam(name = "account", value = "用户名", dataType = "String"),
@ApiImplicitParam(name = "password", value = "密码", dataType = "String"),
})2.3后端Controller层接收代码二
x-www-form-urlencoded:表单的形式,表单格式。可以直接接收DTO数据,方法上不使用@ApiImplicitParams 。直接整个dto接收数据,不需要加上@ReposeBody.按照下方代码接收就可以达到目的
@ApiOperation(value="系统查询", position = 2, notes="系统查询",response =SysUserDTO.class)
@RequestMapping(value = "/query",method = RequestMethod.GET )
public Result query(@Validated ConditionDTO dto, @Validated PageDTO page) throws Exception{
PageResultDTO<SysUserRDTO> result = UserService.query(dto, page);
return Result.success(result);
}3.x-www-form-urlencoded测试软件怎么测试
以下是使用apipost进行测试的示例

总结
到此这篇关于后端如何接收格式为x-www-form-urlencoded数据的文章就介绍到这了,更多相关后端接收x-www-form-urlencoded数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
关于在IDEA中SpringBoot项目中activiti工作流的使用详解
这篇文章主要介绍了关于在IDEA中SpringBoot项目中activiti工作流的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08
SpringMVC与Mybatis集合实现调用存储过程、事务控制实例
这篇文章主要介绍了SpringMVC与Mybatis集合实现调用存储过程、事务控制实例,有需要的可以了解一下。2016-11-11
SpringBoot中的@EnableAutoConfiguration注解解析
这篇文章主要介绍了SpringBoot中的@EnableAutoConfiguration注解解析,@EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义注册到IoC容器,需要的朋友可以参考下2023-09-09
SpringBoot+Redis防止恶意刷新与暴力请求接口的实现
这篇文章主要为大家介绍了如何利用springboot和Redis来实现防止恶意刷新与暴力请求接口,文中的示例代码讲解详细,需要的可以参考一下2022-06-06
基于Spring上下文工具类 ApplicationContextUtil
这篇文章主要介绍了基于Spring上下文工具类 ApplicationContextUtil,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-11-11


最新评论