后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
1.x-www-form-urlencoded是什么?
x-www-form-urlencoded紙面翻譯即所謂url格式的編碼,是post的默認(rèn)Content-Type,其實(shí)就是一種編碼格式,類似json也是一種編碼傳輸格式。form表單中使用
form的enctype屬性為編碼方式,常用有兩種:application/x-www-form-urlencoded和multipart/form-data,默認(rèn)為application/x-www-form-urlencoded。
2.x-www-form-urlencoded類型后端怎么接收
用登錄案例來(lái)作說(shuō)明,登錄的時(shí)候需要輸入賬戶跟密碼。但是前端使用的是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,若有多個(gè)參數(shù)的話使用@ApiImplicition進(jìn)行包裹。
接收x-www-form-urlencoded類型的關(guān)鍵點(diǎn)就在于@ApiImplicitParam。免去了使用@RequestBody在寫一個(gè)接收類的繁瑣步驟,加上@ApiImplicitParam之后直接接收即可。
若使用application/x-www-form-urlencoded類型傳輸數(shù)據(jù)過(guò)來(lái),后端使用@ReposeBody接收,或出現(xiàn)報(bào)錯(cuò)
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
若出現(xiàn)上邊的錯(cuò)誤,只需要按照上邊代碼修改一些接收參數(shù)即可
所以使用application/x-www-form-urlencoded類型傳輸數(shù)據(jù)過(guò)來(lái),需要使用@ApiImplicitParam說(shuō)明參數(shù)并且接收
2.2@ApiImplicitParam說(shuō)明
@ApiImplicitParam是一個(gè)標(biāo)注方法參數(shù)的注解
注解內(nèi)的屬性有
name:參數(shù)名
value:參數(shù)的中文含義
required:是否必須
dataType:參數(shù)類型
paramType:參數(shù)所放位置
defaultValue:參數(shù)的默認(rèn)值
其中,paramType可選值有header、query、path
header標(biāo)注為從@RequestHeader中獲取
query標(biāo)注為從@RequestParam中獲取
path從標(biāo)注為@PathVariable中獲取
方法中有多個(gè)參數(shù)時(shí),使用@ApiImplicitParams包圍
@ApiImplicitParams({
@ApiImplicitParam(name = "account", value = "用戶名", dataType = "String"),
@ApiImplicitParam(name = "password", value = "密碼", dataType = "String"),
})2.3后端Controller層接收代碼二
x-www-form-urlencoded:表單的形式,表單格式??梢灾苯咏邮誅TO數(shù)據(jù),方法上不使用@ApiImplicitParams 。直接整個(gè)dto接收數(shù)據(jù),不需要加上@ReposeBody.按照下方代碼接收就可以達(dá)到目的
@ApiOperation(value="系統(tǒng)查詢", position = 2, notes="系統(tǒng)查詢",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測(cè)試軟件怎么測(cè)試
以下是使用apipost進(jìn)行測(cè)試的示例

總結(jié)
到此這篇關(guān)于后端如何接收格式為x-www-form-urlencoded數(shù)據(jù)的文章就介紹到這了,更多相關(guān)后端接收x-www-form-urlencoded數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于在IDEA中SpringBoot項(xiàng)目中activiti工作流的使用詳解
這篇文章主要介紹了關(guān)于在IDEA中SpringBoot項(xiàng)目中activiti工作流的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
maven打包本地jar到項(xiàng)目中的方法實(shí)現(xiàn)
本文主要介紹了maven打包本地jar到項(xiàng)目中的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
SpringMVC與Mybatis集合實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程、事務(wù)控制實(shí)例
這篇文章主要介紹了SpringMVC與Mybatis集合實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程、事務(wù)控制實(shí)例,有需要的可以了解一下。2016-11-11
SpringBoot中的@EnableAutoConfiguration注解解析
這篇文章主要介紹了SpringBoot中的@EnableAutoConfiguration注解解析,@EnableAutoConfiguration也是借助@Import的幫助,將所有符合自動(dòng)配置條件的bean定義注冊(cè)到IoC容器,需要的朋友可以參考下2023-09-09
SpringBoot+Redis防止惡意刷新與暴力請(qǐng)求接口的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用springboot和Redis來(lái)實(shí)現(xiàn)防止惡意刷新與暴力請(qǐng)求接口,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
基于Spring上下文工具類?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類?ApplicationContextUtil,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Boot緩存實(shí)戰(zhàn) EhCache示例
本篇文章主要介紹了Spring Boot緩存實(shí)戰(zhàn) EhCache示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

