json 如何使用邮递员发布对象和列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31337914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:57:53  来源:igfitidea点击:

How to post object and List using postman

jsonpostman

提问by heshjse

I am using postman packaged appto send a post request.

我正在使用邮递员打包的应用程序发送邮递请求。

I want to request the following controller.

我想请求以下控制器。

How to send a post requestbody using postman object(with values) and a list using the raw format?

如何使用邮递员对象(带值)和使用原始格式的列表发送帖子请求体?

    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage save(@RequestBody Freelancer freelancer, @RequestBody List<Integer> skills) {

I have tried like this :

我试过这样:

{
  "address": "colombo",
  "username": "hesh",
  "password": "123",
  "registetedDate": "2015-4-3",
  "firstname": "hesh",
  "contactNo": "07762",
  "accountNo": "16161",
  "lastName": "jay"
}
{
    "array[0]" :1436517454492,
    "array[1]" :1436517476993
}

回答by Mohsin Muzawar

Make sure that you have made the content-typeas application/jsonin header request and Post from body under the raw tab.

确保您已在原始选项卡下的标题请求和正文中进行了content-typeas application/jsonin 请求。

{
  "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"]


}

回答by Vinod Bokde

Use this Format as per your requirements:

根据您的要求使用此格式:

{
    "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
    ]

}

Remember to use this in POST with proper endpoint. Also, RAW selected and JSON(application/json) in Body Tab.

请记住在具有适当端点的 POST 中使用它。此外,在正文选项卡中选择了 RAW 和 JSON(application/json)。

Like THIS:

像这样:

enter image description here

在此处输入图片说明

Update 1:

更新 1:

I don't think multiple @RequestBody is allowed or possible.

我认为不允许或不可能使用多个 @RequestBody。

@RequestBody parameter must have the entire body of the request and bind that to only one object.

@RequestBody 参数必须包含请求的整个主体并将其绑定到一个对象。

You have to use something like Wrapper Object for this to work.

你必须使用像 Wrapper Object 这样的东西才能工作。

回答by user10784930

i also have a almost same question,it's a example to refer

我也有一个几乎相同的问题,这是一个参考的例子

my controller

我的控制器

@RequestMapping(value = {"/batchDeleteIndex"}, method = RequestMethod.POST)
@ResponseBody
public BaseResponse batchDeleteIndex(@RequestBody List<String> datasetQnames)

postman

邮差

make sure the raw in Body is application/json

确保 Body 中的 raw 是 application/json

["aaa","bbb","ccc"]

回答by GargantuanTezMaximus

I'm not sure what server side technology you are using but try using a json array. A couple of options for you to try:

我不确定您使用的是什么服务器端技术,但请尝试使用 json 数组。几个选项供您尝试:

{
  "address": "colombo",
  "username": "hesh",
  "password": "123",
  "registetedDate": "2015-4-3",
  "firstname": "hesh",
  "contactNo": "07762",
  "accountNo": "16161",
  "lastName": "jay"
 },
[
   1436517454492,
   1436517476993
]

If that doesn't work you may also try:

如果这不起作用,您也可以尝试:

{
  freelancer: {
  "address": "colombo",
  "username": "hesh",
  "password": "123",
  "registetedDate": "2015-4-3",
  "firstname": "hesh",
  "contactNo": "07762",
  "accountNo": "16161",
  "lastName": "jay"
 },
 skills : [
       1436517454492,
       1436517476993
    ]
}

回答by Pinki Bhinder Mittal

In case of simple example if your api is below

如果您的 api 低于简单示例

@POST
    @Path("update_accounts")
    @Consumes(MediaType.APPLICATION_JSON)
    @PermissionRequired(Permissions.UPDATE_ACCOUNTS)
    void createLimit(List<AccountUpdateRequest> requestList) throws RuntimeException;

where AccountUpdateRequest :

其中 AccountUpdateRequest :

public class AccountUpdateRequest {
    private Long accountId;
    private AccountType accountType;
    private BigDecimal amount;
...
}

then your postman request would be: http://localhost:port/update_accounts

那么您的邮递员请求将是: http://localhost:port/update_accounts

[
         {
            "accountType": "LEDGER",
            "accountId": 11111,
            "amount": 100
         },
         {
            "accountType": "LEDGER",
            "accountId": 2222,
            "amount": 300
          },
         {
            "accountType": "LEDGER",
            "accountId": 3333,
            "amount": 1000
          }
]

回答by Hedego

If you use the following format in your request section while making sure the request url is of http://localhost:XXXX/OperationName/V#.

如果您在请求部分使用以下格式,同时确保请求 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"
       },
       ...]
     }

回答by Sana

Try this one,

试试这个,

{
  "address": "colombo",
  "username": "hesh",
  "password": "123",
  "registetedDate": "2015-4-3",
  "firstname": "hesh",
  "contactNo": "07762",
  "accountNo": "16161",
  "lastName": "jay",
  "skill":[1436517454492,1436517476993]
}

回答by shaik

//backend.

//后台。

@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
}

// postman

//邮递员

{
"num":value,
"name":value,
"bList":[{
"key":"value",
"key":"value",.....
}]
}
  1. the error is for list there is no default constructor .so we can keep our list of object as a property of another class and pass the list of objects through the postman as the parameter of the another class.
  1. 错误是因为 list 没有默认构造函数。所以我们可以将我们的对象列表作为另一个类的属性保留,并通过邮递员将对象列表作为另一个类的参数传递。