post接口传参 传集合实体

本文探讨了两种常见的后端数据接收方式:参数拼接与JSON串提交。通过具体实例,比较了这两种方式在实际应用中的优劣,指出JSON方式在复杂数据结构处理上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、通过参数拼接的方式

127.0.0.1:8082/app/exam/addPaperRecord?paperRecord.epId=95&paperRecord.replyTime=180&paperRecord.cusId=861&paperRecord.type=1&paperRecord.testTime=15&optype=0&paperRecord.subjectId=1&paperRecord.id=333&record[0].pointId=1&record[0].qstType=1&record[0].userAnswer=A&record[0].qstIdsLite=132&record[0].score=50&record[0].answerLite=B&record[1].pointId=1&record[1].qstType=1&record[1].userAnswer=A&record[1].qstIdsLite=128&record[1].score=50&record[1].answerLite=D&record[2].pointId=1&record[2].qstType=2&record[2].userAnswer=A,B,C&record[2].qstIdsLite=103&record[2].score=50&record[2].answerLite=CD

接口接收 

    @RequestMapping("/addPaperRecord")
    @ResponseBody
    public Map<String ,Object> addPaperRecord(
                               @ModelAttribute("paperRecord") PaperRecord paperRecord,
                               @RequestParam(required = true) String optype, 
                               QuestionRecordForm questionRecordForm){
            // todo
   }

 集合或数组(前台这样称)如下:

@Data
@EqualsAndHashCode(callSuper = false)
public class QuestionRecordForm {

    private List<QuestionRecordBean> record;

}

 


2、通过json串的方式

前台请求:127.0.0.1:8082/app/exam/saveOrUpdatePaperRecord    方式:post

请求体:

{
    "paperId": 11,
    "replyTime":180,
    "userId":861,
    "paperType":1,
    "testTime": 180,
    "subjectId":1,
    "paperRecordId":28,
    "opType": 0,
    "record":[
       {
        "pointId":1,  
        "qstType":5, 
        "userAnswer": "租入包装物支付的押金,为职工垫付的款项,公积金", 
        "qstIdsLite":17,   
        "score":30,
        "answerLite":"租入包装物支付的押金;为职工垫付的款项;公积金"
       },{}
    ]
}

接口请求使用@RequestBody进行接收 

    @RequestMapping("/saveOrUpdatePaperRecord")
    @ResponseBody
    public Map<String, Object> saveOrUpdatePaperRecord(
                  @RequestBody ExamRecordRq examRecordRq) {
       // todo
    }

参数请求实体 

@Data
public class ExamRecordRq {

    /**
     * 试卷id
     */
    private Long paperId;

    /**
     * 试卷考试时间
     */
    private int replyTime;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 试卷类型
     */
    private int paperType;

    /**
     * 用户测试时间:单位 秒
     */
    private int testTime;

    /**
     * 科目id
     */
    private Long subjectId;

    /**
     * 当前用户试卷记录id
     */
    private Long paperRecordId;

    /**
     * 试卷提交方式 0-交卷 1-下次再做或者临时保存
     */
    private String opType;

    /**
     * 试卷小题集合
     */
    private List<QuestionRecordBean> record;

}

两种方式我更喜欢第二种的json方式提交

 

Java代码调用post接口传参的案例可以分为以下几个步骤: 1. 导相关依赖 一般情况下,我们需要导HTTP客户端的相关依赖,推荐使用Apache HttpComponents或OkHttp来实现HTTP请求。 2. 构造HTTP请求 根据接口文档中给出的API地址和请求方法,我们可以构造对应的HTTP请求对象,设置请求头、数等内容。例如: ``` HttpPost httpPost = new HttpPost("http://example.com/api/user"); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); JSONObject params = new JSONObject(); params.put("name", "张三"); params.put("age", 20); StringEntity stringEntity = new StringEntity(params.toString(), ContentType.APPLICATION_JSON); httpPost.setEntity(stringEntity); ``` 3. 发送请求并处理响应 使用HttpClient或OkHttpClient客户端发送请求,并阻塞等待服务器响应。一般情况下,我们需要根据响应状态码和内容类型等信息来判断请求是否成功,并解析响应内容。 ``` CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); String responseContent = EntityUtils.toString(entity, Charset.forName("UTF-8")); JSONObject responseJson = JSON.parseObject(responseContent); // 处理响应内容 } ``` 以上是Java代码调用post接口传参的基本流程,当然具体实现还需要按照实际需求进行调整。在实际项目开发中,我们也可以使用各种HTTP请求框架封装,遵循开闭原则,提高代码复用性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值