Как передать объект как атрибут другого объекта, используя клиент jersey (jax-rs) в запросе post? - PullRequest
0 голосов
/ 29 апреля 2020

Я не хочу передавать объект, состоящий из другого объекта, как один атрибут, так как этого достичь?

Пример:

Класс

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);

Теперь мне нужно передать obj методу rest API API как объекту [здесь API rest реализован с помощью весенней загрузки]

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

Здесь только с помощью этого метода customerId и customerName будут получены оставшимся API, а объект Product будет иметь нулевое значение.

Пример кода метода оставшегося API

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

   }

Как решить эту проблему?

1 Ответ

0 голосов
/ 08 мая 2020

Здесь для решения этой проблемы я использовал атрибуты, которые мне нужны в классе продукта, в качестве атрибутов в классе CustomerProduct.

Пример:

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

}

Итак, после этого я прошел объект CustomerProduct как отдельный объект для метода post API Rest с использованием jax-rs, как показано ниже.

 CustomerProduct obj = new CustomerProduct(args);

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