Postman传递对象参数
m0_59794830 人气:0项目场景:
postman通常需要传递各式各样的参数,这样的话,进行写参数比较头疼,不知怎么进行传参。
解决方案:
可以考虑将参数对象写成json字符串的样式,然后将将字符串转换成json对象
实体类:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.ibatis.annotations.Param; import org.hibernate.validator.constraints.Length; import org.springframework.validation.annotation.Validated; import javax.persistence.Column; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.Valid; import javax.validation.constraints.Digits; import javax.validation.constraints.NotNull; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor @Table(name = "ssm_funds_main") public class FunsCjyModel implements Serializable { private static final long serialVersionUID = 1L; /** * 主表id */ @Id // @NotNull(message = "id不能为空") @Length(max = 32,message = "主表id长度不能超过32位") @Column(name = "BI_RS_ID") private String biRsId; /** * 所属培训班 */ @NotNull(message = "所属培训班不能为空") @Length(max = 32,message = "培训班长度不能超过32位") @Column(name = "TERM_NAME") private String termName; /** * 发票购买方 */ @NotNull(message = "发票购买方不能为空") @Length(max = 32,message = "发票购买方长度不能超过32位") @Column(name = "BUYER") private String buyer; /** * 发票销售方 */ @NotNull(message = "发票销售方不能为空") @Length(max = 32,message = "发票销售方长度不能超过32位") @Column(name = "SALLER") private String saller; /** * 发票编号 */ @Length(max = 32,message = "发票编号长度不能超过32位") private String billNo; /** * 开票日期 */ private Date billDate; /** * 票面金额 */ @Digits(integer = 8,fraction = 2,message = "票面金额整数上限为8位,小数点上限为2位") private BigDecimal billSum; /** * 复印件路径 */ @Length(max = 100,message = "复印件路径长度不能超过100位") private String picPath; /** * 文件扩展名称 */ @Length(max = 10,message = "文件扩展名称长度不能超过100位") private String picFileExt; /** * 创建人 */ @Length(max = 32,message = "创建人id长度不能超过32位") private String createId; /** * 创建时间 */ private Date createDate; /** * 修改人 */ @Length(max = 32,message = "修改人id长度不能超过32位") private String modifyId; /** * 修改时间 */ private Date modifyDate; /** * 从表集合 */ @Transient @Valid private List<DetailCjyModel> list; }
参数样式:
{ "biRsId":"", "termName":"实训", "buyer":"学生", "saller":"学校", "billNo":"20210722", "billSum":"900.00", "createId":"CJY", "list":[ { "rsId":"", "itemName":"语文", "itemSpec":"私人", "itemUnit":"元", "qty":"10.00", "price":"30.00", "subSum":"300.00", "taxPercent":"0.1", "taxAtm":"30.0" }, { "rsId":"", "itemName":"数学", "itemSpec":"集体", "itemUnit":"元", "qty":"20.00", "price":"30.00", "subSum":"600.00", "taxPercent":"0.1", "taxAtm":"60.0" } ] }
控制层代码:
@RequestMapping("/insertFunsDetailTwo") @ResponseBody public ResultMap insertFunsDetailTwo(String dataStr,@RequestParam (value = "file",required = false) MultipartFile file){ JSONObject dataStrMap = JSONObject.parseObject(dataStr); FunsCjyModel funsCjyModel = new FunsCjyModel(); funsCjyModel.setTermName((String)dataStrMap.get("termName")); funsCjyModel.setBuyer((String)dataStrMap.get("buyer")); funsCjyModel.setSaller((String)dataStrMap.get("saller")); funsCjyModel.setBillNo((String)dataStrMap.get("billNo")); funsCjyModel.setBillSum(funsModelCjyServiceImpl.stringBigeDecimal((String)dataStrMap.get("billSum"))); funsCjyModel.setCreateId((String)dataStrMap.get("createId")); String list1 = JSON.toJSONString(dataStrMap.get("list")); List<DetailCjyModel> list = JSONArray.parseArray(list1,DetailCjyModel.class); funsCjyModel.setList(list); return funsModelCjyService.insertFunsAndDetail(funsCjyModel,file); }
这样的话可以用postman的form-data传文件和集合对象的参数
补充:Postman测试接口传递对象参数
url:
使用的是post方式请求
在Headers设置:
在Body写入对象信息,主要红线的地方:
加载全部内容