如何在邮递员中将两个不同的对象传递给POST请求以测试RESTful API?

时间:2019-05-27 09:33:36

标签: json rest spring-boot postman

这实际上是一个两部分的问题,一个是如何传递两个JSON对象,另一个是如何从两个不同的类创建对象并将该对象传递给POST方法。

我试图通过在邮递员中传递两个不同的对象来测试RESTful API的POST方法。我只能成功地测试一个对象的POST方法,但是当我尝试将两个不同的对象发送到其中两个不同的对象的POST方法时,邮递员说的是错误的请求。

这是我的两个实体类:第一个是Cleint类:

import lombok.Data;
import org.zevgma.api.entities.user.User;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity
@Table(name = "client")
public class Client {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_pk_generator")
    @SequenceGenerator(name = "user_pk_generator", sequenceName = "users_user_id_seq", allocationSize = 1)
    @Column(name = "id", unique = true, nullable = false,  columnDefinition = "serial")
    private int id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private Date userDateCreate;

 }

第二个关于ClientAddress:

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity
public class ClientAddress {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int streetID;
    private String streetName;
    private int streetNumber;
}

我在Stackoverflow和youtube中尝试了带有JSON objets或JSON嵌套线程的其他POST请求,但是找不到我的解决方案。 我特别尝试了这些线程中的指令:

这是我要测试的RESTful API的POST方法:

@PostMapping(value = "/api/employees", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createClient(@RequestBody Client client,
                                           @RequestBody ClientAddress clientAddress){
    clientService.save(client);
    clientAddressService.save(clientAddress);
    return new ResponseEntity<>(client.getName() + " and "+ clientAddress.getStreetName() + " successfully added",
            HttpStatus.OK);
}

到目前为止,我已经尝试过使用此JSON对象:

[
{
     "name": "Bilbo",
      "email": "bilbo@gmail.com"
    },
		
  {
     "streetName": "Central Street",
     "streetNumber": "31"
   }
]

[
	{
		"client":{
               "name": "Bilbo",
                "email": "bilbo@gmail.com"
              },
		
		"clientAddress":{
                      "streetName": "Central Street",
                      "streetNumber": "31"
                     }
	  }
]

通过预请求脚本:

 //Pre-request Script
 var client = {
     "name": "Bilbo",
     "email": "bilbo@mail.com"
 }

var address = {
    "streetName": "New Street",
    "streetNumber": "3"
}

pm.environment.set("client", JSON.stringify(client));
pm.environment.set("address", JSON.stringify(address));

//Body
[
{{client}},{{address}}	
]

现在问题的第二部分:如果我想从上面提到的两个类Client和ClientAddress中创建一个新对象ClientData并将该ClientData对象传递给POST方法,该怎么做。

这是ClientData类:

导入lombok.Data;

@Data
public class ClientData {

    private Client client;
    private ClientAddress address;
}

这是我要传递ClientData对象的POST方法:

  @PostMapping(value = "/api/employees", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> createClient(@RequestBody ClientData clientData){
               Client client = clientData.getClient();
                clientService.save(client);
            ClientAddress clientAddress = clientData.getAddress();
            clientAddressService.save(clientAddress);

        return new ResponseEntity<>(client.getName() + " and "+ clientAddress.getStreetName() + " successfully added",
                HttpStatus.OK);
    }

要测试邮递员中的方法,我应该创建这些复杂的ClientData JSON对象以及如何传递它?

2 个答案:

答案 0 :(得分:1)

您的ClientData类是

@Data
public class ClientData {

    private Client client;
    private ClientAddress address;
}

所以您要求的有效载荷应该看起来像

    {
        "client":{
               "name": "Bilbo",
                "email": "bilbo@gmail.com"
              },

        "address":{
                      "streetName": "Central Street",
                      "streetNumber": "31"
                     }
      }

通知地址,而不是clientAddress。 另外,您也不需要将其作为数组传递,它只是一个json对象(不是[])

答案 1 :(得分:0)

而不是在体内这样做:

image: gcc

build:
  stage: build
  script:
  - ./configure
  - mkdir build && cd $_
  - cmake ..
  - make -sj8
artifacts:
  paths:
  - "build/*.elf"

deploy:
  image: artifactory
  variables:
  - DO_NOT_CLONE: 1  ## WANT THIS OPTION
  stage: deploy
  script:
  - push_artifacts build/*.elf

替换为:

[{{client}}, {{address}}]

现在,将整个正文存储在环境变量中。 删除用于客户端和地址的单独变量,仅使用一个变量即可。

因此,将您的请求前脚本更新为以下内容:

{{requestBody}}