【问题标题】:Passing a class object as a parameter in Postman在 Postman 中将类对象作为参数传递
【发布时间】:2020-11-25 16:43:09
【问题描述】:

我正在开发一个 API 应用程序,但是我在使用特定方法时遇到了困难。

getCertificationPOJO 方法,负责将 Store 作为参数,并搜索该 Store 是否存在于数据库中。

现在这里的问题是如何在 Postman 中将 Store 对象作为参数传递。我尝试将其作为 JSON 字符串传递,但这不起作用。

抱歉编辑不当

认证控制人

@Controller
public class CertController {

    @Autowired
    private CertificationRepository certRepo;
    @Autowired
    private StoreRepository StoreRepository;

    
    @GetMapping(value = "/getCertObject")
    public @ResponseBody
    Optional<Certification> getCertificationPOJO(@RequestParam Store store)
    {return Lists.newArrayList(certRepo.findAll()).stream().filter(e->e.getStore() == store).findFirst();}

}

商店类

@Entity
@Table(name = "store")
public class Store implements com.halal.abstractions.Entity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @JsonIgnore
    @OneToOne(optional = true) // By default this is set to true so technically this is redundant but, yea lets
                               // just keep it there
    private Certification certification;

    @NotNull
    @Column(nullable = false)
    private String name;

    private String phoneNumber;

    @NotNull
    @Column(nullable = false)
    private String address;

    @NotNull
    @Column(nullable = false)
    private double latitude;

    @NotNull
    @Column(nullable = false)
    private double longitude;

    @NotNull
    @Column(nullable = false)
    private Date dateAdded;

    static final DateFormat DF = new SimpleDateFormat("dd/MM/yyyy");

    protected Store() {
    }

    public Store(String name, String phoneNumber, String address, double latitude, double longitude) {
        this.name = name;
        this.setPhoneNumber(phoneNumber);
        this.setAddress(address);
        this.latitude = latitude;
        this.longitude = longitude;
        this.dateAdded = new Date(System.currentTimeMillis());
    }
    @Override
    public Long getId() {
        return this.id;
    }
    @Override
    public void setId(long id) {
        this.id = id;
    }
    @Override
    public String getName() {
        return name;
    }
    @Override
    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    @Override
    public String getDateAdded() {
        return DF.format(dateAdded);
    }

    @Override
    public void setCertification(Certification certification) {
        this.certification = certification;
    }
    @Override
    public Certification getCertification() {
        return this.certification;
    }

    @Override
    public String toString() {
        return "Store{" + "id=" + id + ", certification=" + certification + ", name='" + name + '\'' + ", phoneNumber='"
                + phoneNumber + '\'' + ", address='" + address + '\'' + ", latitude=" + latitude + ", longitude="
                + longitude + ", dateAdded=" + dateAdded + '}';
    }
}

认证等级

@Entity
@Table(name = "certification")
public class Certification {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotNull
    @Column(nullable = false)
    private boolean isCertified;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    @JsonIgnore
    private Store store;

    public Certification() {
    }

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Store getStore() {
        return this.store;
    }

    public void setStore(Store store) {
        this.store = store;
    }

    public boolean isIsCertified() {
        return this.isCertified;
    }

    public void setIsCertified(boolean isCertified) {
        this.isCertified = isCertified;
    }

    @Override
    public String toString() {
        return "Certification{" + "id=" + id + ", isCertified= " + isCertified + '}';
    }
}

【问题讨论】:

  • 请忽略“addStuff”标题,我们没有向数据库提交任何内容。

标签: java spring-boot rest postman


【解决方案1】:

在这种情况下,您不使用 Params 部分。你使用Body。单击该选项卡并将您的 JSON OBJECT 放入将出现的文本框中。您还需要通过在显示在附件图像右侧最远的 COMBO 中选择适当的值来将 CONTENT TYPE 设置为 JSON。

看看这个:

【讨论】:

  • 我还需要改变我的方法吗?
  • 这不起作用我得到一个错误“已解决[org.springframework.web.bind.MissingServletRequestParameterException:必需的实体参数'store'不存在]”
【解决方案2】:

如果您使用的是 queryParams,则必须单独传递每个键/参数,在您的 Postman 中您可以像这样填写:

key: id          value: 1
key: name        value "asdasd"
key: phoneNumber value: "000"

(...)

另一种选择是您更改策略并在 Body 中传递整个 json,但您必须更改 RestController 以接收 @RequestBody 而不是 @RequestParam

【讨论】:

    【解决方案3】:

    我建议不要在 GET 请求中发送正文,请参阅类似的答案 here

    @Murilo 建议的查询参数路由是一种方式,但如果确定数据库中是否已存在 getCertificationPOJO 的方式仅取决于 id,那么您可能只需要发送 id 在这种情况下路径变量将是最好如下所示,其中 {id} 可以替换为实际 ID

    GET /getCertObject/{id}
    

    然后在控制器中

    @GetMapping("/getCertObject/{id}")
    @ResponseBody
    Optional<Certification> getCertificationPOJO(@PathVariable String id) {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      相关资源
      最近更新 更多