如何使用邮递员发布对象和列表
Posted
技术标签:
【中文标题】如何使用邮递员发布对象和列表【英文标题】:How to post object and List using postman 【发布时间】:2015-09-29 00:49:29 【问题描述】:我正在使用postman packaged app 发送帖子请求。
我想请求以下控制器。
如何使用邮递员对象(带值)和使用原始格式的列表发送帖子请求正文?
@RequestMapping(value = "register", method = RequestMethod.POST)
@ResponseBody
public ResponseMessage save(@RequestBody Freelancer freelancer, @RequestBody List<Integer> skills)
我试过这样:
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay"
"array[0]" :1436517454492,
"array[1]" :1436517476993
【问题讨论】:
【参考方案1】:
"preOrderData" : [
"pname": "xyz",
"quantity": "1",
"unit": "Peice",
"description": "xyz 100 gram",
"preferred_brand": "xyz",
"entry_date": "2020-10-05 11:11:27",
"creation_date": "2020-10-05 11:11:27",
"updated_date": "2020-10-05 11:11:27",
"user": "ABC@gmail.com",
"user_type": "individual"
,
"productname": "abc cream",
"quantity": "1",
"unit": "Peice",
"description": "abc 100 gram",
"preferred_brand": "abccream",
"entry_date": "2020-10-05 11:11:27",
"creation_date": "2020-10-05 11:11:27",
"updated_date": "2020-10-05 11:11:27",
"user": "xyz@gmail.com",
"user_type": "individual"
]
【讨论】:
通过req.body.preOrderData获取数据【参考方案2】:我也有类似的问题,如果有帮助,请分享以下示例。
我的控制器:
@RequestMapping(value = "/batchDeleteIndex", method = RequestMethod.POST)
@ResponseBody
public BaseResponse batchDeleteIndex(@RequestBody List<String> datasetQnames)
邮递员: 将 Body 类型设置为 raw 并添加 header Content-Type: application/json
["aaa","bbb","ccc"]
【讨论】:
【参考方案3】:如果你的 api 低于这个简单的例子
@POST
@Path("update_accounts")
@Consumes(MediaType.APPLICATION_JSON)
@PermissionRequired(Permissions.UPDATE_ACCOUNTS)
void createLimit(List<AccountUpdateRequest> requestList) throws RuntimeException;
在哪里 AccountUpdateRequest :
public class AccountUpdateRequest
private Long accountId;
private AccountType accountType;
private BigDecimal amount;
...
那么您的邮递员请求将是: http://localhost:port/update_accounts
[
"accountType": "LEDGER",
"accountId": 11111,
"amount": 100
,
"accountType": "LEDGER",
"accountId": 2222,
"amount": 300
,
"accountType": "LEDGER",
"accountId": 3333,
"amount": 1000
]
【讨论】:
【参考方案4】:根据您的要求使用此格式:
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay"
"arrayOneName" : [
"Id" : 1,
"Employee" : "EmpOne",
"Deptartment" : "HR"
,
"Id" : 2,
"Employee" : "EmpTwo",
"Deptartment" : "IT"
,
"Id" : 3,
"Employee" : "EmpThree",
"Deptartment" : "Sales"
],
"arrayTwoName": [
"Product": "3",
"Price": "6790"
],
"arrayThreeName" : [
"name1", "name2", "name3", "name4" // For Strings
],
"arrayFourName" : [
1, 2, 3, 4 // For Numbers
]
记得在 POST 中使用它并带有正确的端点。此外,在正文选项卡中选择了 RAW 和 JSON(application/json)。
喜欢这个:
更新 1:
我认为不允许或不可能使用多个 @RequestBody。
@RequestBody 参数必须包含整个请求体,并且只绑定到一个对象。
你必须使用像 Wrapper Object 这样的东西才能工作。
【讨论】:
【参考方案5】:试试这个,
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay",
"skill":[1436517454492,1436517476993]
【讨论】:
【参考方案6】:如果您在请求部分使用以下格式,同时确保请求 URL 为 http://localhost:XXXX/OperationName/V#。
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay",
"listName":[
"elementOne":"valueOne"
,
"elementTwo":"valueTwo"
,
...]
【讨论】:
嗨!由于其长度和内容,该答案在审查中被认为质量低下。发生这种情况是因为 SO 上通常不鼓励仅使用代码回答,并且应附有一些文字说明,说明代码如何解决问题。谢谢! 相应更新。谢谢。【参考方案7】://后端。
@PostMapping("/")
public List<A> addList(@RequestBody A aObject)
//......ur code
class A
int num;
String name;
List<B> bList;
//getters and setters and default constructor
class B
int d;
//defalut Constructor & gettes&setters
//邮递员
"num":value,
"name":value,
"bList":[
"key":"value",
"key":"value",.....
]
-
错误是列表没有默认构造函数。因此我们可以将对象列表保留为另一个类的属性,并通过邮递员将对象列表作为另一个类的参数传递。
【讨论】:
【参考方案8】:确保您已在标头请求中将content-type
设置为application/json
,并在原始选项卡下的正文中发布。
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay",
"arrayObjectName" : [
"Id" : 1,
"Name": "ABC" ,
"Id" : 2,
"Name" : "XYZ"
],
"intArrayName" : [111,222,333],
"stringArrayName" : ["a","b","c"]
【讨论】:
【参考方案9】:我不确定您使用的是哪种服务器端技术,但请尝试使用 json 数组。您可以尝试几个选项:
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay"
,
[
1436517454492,
1436517476993
]
如果这不起作用,您也可以尝试:
freelancer:
"address": "colombo",
"username": "hesh",
"password": "123",
"registetedDate": "2015-4-3",
"firstname": "hesh",
"contactNo": "07762",
"accountNo": "16161",
"lastName": "jay"
,
skills : [
1436517454492,
1436517476993
]
【讨论】:
错误:无法为两者检索请求的 URL 这听起来像是服务器上的问题。请确保服务已启动并正在运行。以上是关于如何使用邮递员发布对象和列表的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 nodejs 在 mailchimp 订阅者列表中创建标签?
如何将文件和 json 对象从邮递员传递到 asp.net 核心 webapi