在Postman中将类对象作为参数传递

huangapple go评论85阅读模式
英文:

Passing a class object as a parameter in Postman

问题

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

getCertificationPOJO 方法负责接受一个 Store 对象作为参数,并在数据库中搜索该商店是否存在。

现在问题在于,我如何在 Postman 中将 Store 对象作为参数传递。我尝试将其作为 JSON 字符串传递,但没有成功。

抱歉编辑不佳。

Certification Controller

@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();}

}

Store 类

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

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

    @JsonIgnore
    @OneToOne(optional = true)
    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;
    }

    // 其他 getter 和 setter 方法
}

Certification 类

@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() {
    }

    // 其他 getter 和 setter 方法
}

在Postman中将类对象作为参数传递

英文:

i am developing an API application, however I am having difficulties with a particular method.

The getCertificationPOJO method, is responsible for taking in a Store as a parameter and searching if that store exists in the database.

Now the problem here is how do I pass a Store object as a parameter in Postman. I have tried passing it in as a JSON string, but that is not working.

Apologies for the bad edit

Certification Controller

@Controller
public class CertController {
@Autowired
private CertificationRepository certRepo;
@Autowired
private StoreRepository StoreRepository;
@GetMapping(value = &quot;/getCertObject&quot;)
public @ResponseBody
Optional&lt;Certification&gt; getCertificationPOJO(@RequestParam Store store)
{return Lists.newArrayList(certRepo.findAll()).stream().filter(e-&gt;e.getStore() == store).findFirst();}
}

Store Class

@Entity
@Table(name = &quot;store&quot;)
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(&quot;dd/MM/yyyy&quot;);
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 &quot;Store{&quot; + &quot;id=&quot; + id + &quot;, certification=&quot; + certification + &quot;, name=&#39;&quot; + name + &#39;\&#39;&#39; + &quot;, phoneNumber=&#39;&quot;
+ phoneNumber + &#39;\&#39;&#39; + &quot;, address=&#39;&quot; + address + &#39;\&#39;&#39; + &quot;, latitude=&quot; + latitude + &quot;, longitude=&quot;
+ longitude + &quot;, dateAdded=&quot; + dateAdded + &#39;}&#39;;
}
}

Certification Class

@Entity
@Table(name = &quot;certification&quot;)
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 = &quot;id&quot;)
@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 &quot;Certification{&quot; + &quot;id=&quot; + id + &quot;, isCertified= &quot; + isCertified + &#39;}&#39;;
}
}

在Postman中将类对象作为参数传递

答案1

得分: 1

我建议不要在GET请求中发送请求体,请参考这里的类似回答

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

GET /getCertObject/{id}

然后在控制器中:

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

I would advise against sending a body in a GET request, see similar answer here

The query param route that @Murilo suggests is one way but if the way of determining whether a getCertificationPOJO already exists in the database depends solely on the id then you may only need to send the id in which case a path variable would be best like below where {id} can be replaced with the actual ID

GET /getCertObject/{id}

and then in the controller

@GetMapping(&quot;/getCertObject/{id}&quot;)
@ResponseBody
Optional&lt;Certification&gt; getCertificationPOJO(@PathVariable String id) {
...
}

答案2

得分: 0

你在这种情况下不使用“Params”部分。你要使用“Body”部分。点击该选项卡,然后将你的JSON对象放入将出现的文本框中。你还需要通过选择出现在附图最右边的COMBO中的适当值来将CONTENT TYPE设置为JSON。

查看这个:

在Postman中将类对象作为参数传递

英文:

You don't use Params section in this case. You use Body. Click on that tab and put your JSON OBJECT in the textbox that will appear. You also need to set CONTENT TYPE to JSON by selecting the proper value in the COMBO that appears farthest to the right of the attached image.

Check this out:

在Postman中将类对象作为参数传递

答案3

得分: 0

如果您正在使用queryParams,则必须逐个传递每个键/参数,在Postman中,您可以填写如下:

键:id          值:1
键:name        值:“asdasd”
键:phoneNumber 值:“000”
(...)

另一个选择是更改您的策略,将整个json传递到您的请求体中,但是您将不得不将您的RestController更改为接收@RequestBody而不是@RequestParam。

英文:

If you are using queryParams you have to pass individually each key/param, in your Postman you fill like it:

key: id          value: 1
key: name        value &quot;asdasd&quot;
key: phoneNumber value: &quot;000&quot;
(...)

Another option is you change your strategy and pass the entire json in your Body, but you will have to change your RestController to receive a @RequestBody instead of @RequestParam

huangapple
  • 本文由 发表于 2020年8月5日 21:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63266310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定