前台请求格式对应后台接收数据记录(包括postman)

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,
                        }),
                        // dataType: "json",
                        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;
}

//接口  使用@RequestBody来接收数据
@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)
//前台
//data
dataItemAddDTO: {
                name: '',
                status: 'Public',
                description: '',
                detail: '',
                author: '',
                reference: '',
                keywords: [],
                classifications: [],
                displays: [],
                authorship: [],
                meta: {
                    coordinateSystem: '',
                    geographicProjection: '',
                    coordinateUnits: '',
                    boundingRectangle: []
                }

            },
//methods层
this.dataItemAddDTO.authorship = authorship;
this.dataItemAddDTO.userDataList = this.userDataList;
*****
//后台DTO
@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;

}
//post向后台  使用@RequestBody接收数据
@RequestMapping (value = "", method = RequestMethod.POST)
    JsonResult add(@RequestBody DataItemAddDTO dataItemAddDTO) {
        return ResultUtils.success(dataItemService.insert(dataItemAddDTO));
    }
//Service层处理数据,进行诸如入库等操作
  • 利用application/json传输特定格式的数据
    可以利用DTO来进行数据的规范传输
    (注释@RequestBody)
//首先引入jar包
<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();
                        }
                    })
//后台DTO
@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) {
    ***
    }
    //这种结构的使用@RequestBody注释进行接收
  • postman测试接口类
    • 第一种 数据传输格式使用formdata
      • 1.上传包括多个文件在内的各种数据以及参数
        (注释@RequestParam)
        postman上传
        使用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)List格式传输
        使用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
        使用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()可以有多个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值