Postman请求格式对应后台接收数据汇总
前台data格式为一种复杂数据结构的,例如包含List以及单个属性,这时候可以写固定格式的entity来进行传输 (注释@RequestBody)
var oids
=[{}];
for (let i
=0;i
<this.community_tableData3_self
.length
;i
++){
if (this.model_tableData3_self
[i
].readStatus
== 0) {
var object
={};
object
.oid
= this.community_tableData3_self
[i
].oid
;
object
.type
= this.community_tableData3_self
[i
].type
;
oids
.push(object
);
}
}
$
.ajax({
url
:"/version/MyEditionReaded",
type
:"POST",
contentType
:"application/json",
data
:JSON.stringify({
my_edition_num
:this.community_reject_unread
,
oids
:oids
,
}),
success
: (json
) => {
if (json
=="ok"){
*****
}
})
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Oids {
String oid
;
String type
;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyEdition {
int my_edition_num
;
List
<Oids> oids
;
}
@RequestMapping(value
= "/MyEditionReaded",method
= RequestMethod
.POST
)
public String
MyEditionReaded(HttpServletRequest request
,@RequestBody MyEdition myEditions
){
***
}
使用DTO格式来进行接收数据 前台更为复杂数据,包含各种类型的字段的数据,诸如List、 List (FileMetaUser是一种数据结构的实体)、int、String等,这时候在前台需要将各种类型数据放置到与后台所设DTO数据结构一致的data中,再在后台使用DTO进行接收(DTO层用来规范接收数据) (注释@RequestBody)
dataItemAddDTO
: {
name
: '',
status
: 'Public',
description
: '',
detail
: '',
author
: '',
reference
: '',
keywords
: [],
classifications
: [],
displays
: [],
authorship
: [],
meta
: {
coordinateSystem
: '',
geographicProjection
: '',
coordinateUnits
: '',
boundingRectangle
: []
}
},
this.dataItemAddDTO
.authorship
= authorship
;
this.dataItemAddDTO
.userDataList
= this.userDataList
;
*****
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataItemAddDTO {
String name
;
String status
;
String contentType
;
String description
;
String detail
;
String author
;
String reference
;
List
<String> keywords
;
List
<String> classifications
;
List
<String> displays
;
List
<AuthorInfo> authorship
;
List
<FileMetaUser> userDataList
;
String dataType
;
List
<DataMeta> dataList
;
int shareCount
=0;
int viewCount
=0;
int thumbsUpCount
=0;
DataItemMeta meta
;
}
@RequestMapping (value
= "", method
= RequestMethod
.POST
)
JsonResult
add(@RequestBody DataItemAddDTO dataItemAddDTO
) {
return ResultUtils
.success(dataItemService
.insert(dataItemAddDTO
));
}
略
利用application/json传输特定格式的数据 可以利用DTO来进行数据的规范传输 (注释@RequestBody)
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>fastjson
</artifactId
>
<version>1.2.3</version
>
</dependency
>
let data
={
modifier
:tableItem
.modifier
.modifier
,
type
:tableItem
.type
,
oid
:tableItem
.oid
,
originOid
:tableItem
.originOid
};
$
.ajax({
type
:"POST",
url
:"/version/accept",
contentType
: "application/json",
data
: JSON.stringify(data
),
async: true,
success
:(json
)=>{
window
.location
.reload();
}
})
@Data
@AllArgsConstructor
@NoArgsConstructor
public class VersionDTO {
String modifier
;
String type
;
String oid
;
String originOid
;
}
@RequestMapping(value
= "/accept", method
= RequestMethod
.POST
)
public JsonResult
accept(@RequestBody VersionDTO versionDTO
) {
***
}
postman测试接口类
第一种 数据传输格式使用formdata
1.上传包括多个文件在内的各种数据以及参数 (注释@RequestParam) 使用formdata格式传输接收时,使用@RequestParam注释进行接收
@RequestMapping(value
= "/data", method
= RequestMethod
.POST
)
public JsonResult
uploadFile(@RequestParam("ogmsdata")MultipartFile
[] files
,
@RequestParam("name")String uploadName
,
@RequestParam("userId")String userName
,
@RequestParam("serverNode")String serverNode
,
@RequestParam("origination")String origination
) throws IOException
, DocumentException
{
***
}
postman测试接口类
第一种 数据传输格式使用formdata
2.利用formdata格式的传输ArrayList的数组数据 (注释@RequestParam) 使用formdata格式传输接收时,使用@RequestParam注释进行接收
@RequestMapping(value
= "/bindDataItem", method
= RequestMethod
.POST
)
public JsonResult
bingDataItem(@RequestParam (value
= "dataIds") ArrayList
<String> dataIds
,
@RequestParam(value
= "proId") String proId
,
@RequestParam(value
= "proName") String proName
,
@RequestParam(value
= "proDescription") String proDescription
,
@RequestParam(value
= "token") String token
,
@RequestParam(value
= "parameter") ArrayList
<String> parameter
){
***
}
postman测试接口类
第二种 数据传输格式也是使用application/json
利用application/json格式,传输json数据,通过这种方式可以传输List数据,在postman中选择raw进行填写json串 使用application/json格式传输接收时,使用@RequestBody注释进行接收
@RequestMapping(value
= "/BulkDownLoad",method
= RequestMethod
.GET
)
public void BulkDownLoad(@RequestBody List
<String> uids
, HttpServletResponse response
) throws UnsupportedEncodingException
{
***
}
未完待续
2020.07.31补充
@RequestBody理解补充 @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。 前台:
$
.ajax({
url
:"/dataItem/getDataApplication",
type
:"POST",
data
:categoryId
,
success
:json
=>{
that
.dataApplication
= json
.data
;
console
.log(that
.dataApplication
);
}
})
请求方式为post,将id传输到后台 后台:
@RequestMapping(value
= "/getDataApplication",method
= RequestMethod
.POST
)
public JsonResult
getDataApplication(@RequestBody String categoryId
){
****
}
@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个