1

I am trying to fetch product data based on some request criteria. When I am converting the productmodel to productdata using convertors and populators i am getting all the product data in response.

I had tried to set the string value while converting & populating the productmodel data to productdata but its not helpful!!

{
     "products": [
         {
           //Getting from Product Model
           "name" : "ABC"
           "desc" : "abcde"
           "Quantity": 2"

           //Not from Product Model
           "matcode" : "001100"
         },
     ]
 }

is it possible to set one more string value(String matcode ="ABC") inside the same response?

HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
ASMA2412
  • 61
  • 1
  • 5

3 Answers3

1

Ideally, if you set matcode(a attribute) in ProductData correctly it gets reflected in the response

Decare matcode attribute inside ProducctData by declaring it in your *beans.xml, something like.

<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
    <!-- other attributes -->
    <property name="matcode" type="java.util.Set&lt;java.lang.String>"/>
</bean>

Now inside populator, set the matcode attribute value and you are done. Debug your controller and see whether you custom attribute value is there in product data. If it's there then it'll get converted to JSON correctly.

@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
{
    //... other codes
    productData.setMatcode("001100"); // add your logic to set this value
}
HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
  • Just a note since the title mentions JSON. To add the attribute to your OCC webservices, you need to define the attribute to ProductWsDTO in *commercewebservices-beans.xml and add a mapping to dto-level-mappings-v2-spring.xml. – Johannes von Zmuda Jun 19 '19 at 14:19
  • Yeah, but here SO was trying to get ProductData as Json Obj on the storefront – HybrisHelp Jun 20 '19 at 02:21
0

If you're using hql, you can do it like:

@Entity
@Table(name = "product")
public class Product {

      @Column(name = "name")
      private String name;

      @Column(name = "desc")
      private String desc;

      @Column(name = "quantity")
      private Integer quantity;

      @Transient
      @Column(name = "quantity")
      private String matcode;

      public Product(String name, String desc, Integer quantity, String matcode) {
         this.name = name;
         this.desc = desc;
         this.quantity = quantity;
         this.matcode = matcode;
     }

}

If you want to read more about Transient annotation, please follow Transient Attribute

Tayyab Razaq
  • 348
  • 2
  • 11
0

You can use a library like gson. Assuming you have model like this:

public class Products
{
    private List<Product> products;
}


public class Product
{
    private String name;

    private String desc;

    private String Quantity;
}

Easy way:

Add another attribute to Product model

  private String matcode;

Now below code could be used:

  Gson gson = new Gson();
  String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
  Products products = gson.fromJson(jsonOutput, Products.class);
  System.out.println(products);

  for(Product p : products.getProducts()){
    p.setMatcode("001100");
  }

 System.out.println(gson.toJson(products));

Another longer path :

a. read the JSON response b. convert to object ( which you should be already doing ) c. use gson to convert object to JsonElement as array d. iterate and update the JsonObject as you require e. convert the updated JsonElement to String output.

Working code below:

 Gson gson = new Gson();
 String jsonOutput = "{\"products\": [{ \"name\" : \"ABC\" ,\"desc\" : \"abcde\", \"Quantity\": \"2\"}]}";
 Products products = gson.fromJson(jsonOutput, Products.class);
 System.out.println(products);

 JsonElement jsonElement = gson.toJsonTree(products);
 JsonArray jsonArray = jsonElement.getAsJsonObject().get("products").getAsJsonArray();
 for (JsonElement ele : jsonArray) {
     JsonObject obj = ele.getAsJsonObject();
     obj.addProperty("matcode", "001100");
 }
 String updatedJsonOutput = gson.toJson(jsonElement); 
 System.out.println("Updated json Object: " + updatedJsonOutput);
TechFree
  • 2,600
  • 1
  • 17
  • 18