简体   繁体   English

如何在发布请求中使用 jersey(jax-rs) 客户端将 object 作为另一个 object 的属性传递?

[英]How to pass an object as an attribute of another object using jersey(jax-rs) client in a post request?

I wan't to pass an object which consist of another object as it's one attribute,so how to achieve this?我不想传递一个包含另一个 object 的 object 作为一个属性,那么如何实现呢?

Ex:前任:

Class Class

public class CustomerProduct{

         private int customerId;
         private String customerName;
         private Product product;

        //Constructors and getters setters


         } 
         Product p1 = new Product(with required args);
         CustomerProduct obj = new CustomerProduct(1,"John",p1);

Now I need to pass the obj to the rest API post method as an object[Here rest API is implemented by spring boot] Now I need to pass the obj to the rest API post method as an object[Here rest API is implemented by spring boot]

Response response1 =client.target(endPointUrl).request().post(Entity.json(obj));

Here with the aid of this method only the customerId and customerName will be receive by the rest API and the Product object will be get as null.在此方法的帮助下,rest API 将仅接收 customerId 和 customerName,而 Product object 将作为 Z38DFF7A6725ECCBD24

Sample code of rest API method rest API方法示例代码

@PostMapping("/customer-product")
   public void sampleControllerMethod(@RequestBody CUstomerProductDTO customerProductDTO){
        customerProductService.methodA(customerProductDTO);

   }

How to solve this?如何解决这个问题?

In here What I did to solve this issue was using the attributes that I needed in product class as attributes in CustomerProduct class.在这里,我为解决此问题所做的是使用产品 class 中所需的属性作为 CustomerProduct class 中的属性。

Example:例子:

public class CustomerProduct{

    private int customerId;
    private String customerName;

    //Using the attributes in Product class as attributes in CustomerProduct class

    private String productName;
    private String productPrice;
    //Likewise I used product object attributes inside the CustomerProduct class 

    //Overloaded constructors and getters setters

}

So after that I passed the object of CustomerProduct as a single object to the post method of Rest API using jax-rs like below.因此,在那之后,我将 CustomerProduct 的 object 作为单个 object 传递给 Rest ZDB974238714CA8DE6347 之类的 post 方法,如下面的 CEja1x-rsa 。

 CustomerProduct obj = new CustomerProduct(args);

 Response response1 =client.target(endPointUrl).request().post(Entity.json(obj));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM