【问题标题】:How to populate a custom attribute in ProdctData JSON?如何在 ProdctData JSON 中填充自定义属性?
【发布时间】:2019-10-28 17:18:33
【问题描述】:

我正在尝试根据某些请求条件获取产品数据。当我使用转换器和填充器将产品模型转换为产品数据时,我得到了所有产品数据作为响应。

我曾尝试在将 productmodel 数据转换并填充到 productdata 时设置字符串值,但它没有帮助!!

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

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

是否可以在同一个响应中再设置一个字符串值(String matcode ="ABC")?

【问题讨论】:

  • 意思是,你想在上面给出的现有json中再追加一个属性matcode
  • @TayyabRazaq 是的,但 productmodel 中不存在
  • 好的,如果不在产品模型中也没问题,但是您可以扩展产品模型。
  • 您是否获得了响应 http 调用的产品模型?其次,matcode的值怎么设置?
  • @TayyabRazaq 是的,我正在获取 productmodel 数据属性作为响应....我无法设置 matcode,所以如果可能的话,您需要有关如何执行此操作的帮助?

标签: java json hybris populate converters


【解决方案1】:

理想情况下,如果您在 ProductData 中正确设置 matcode(a 属性),它会反映在响应中

通过在 *beans.xml 中声明它来去除 ProducctData 中的 ma​​tcode 属性,类似于。

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

现在在填充器中,设置matcode 属性值,你就完成了。调试您的控制器并查看您的自定义属性值是否存在于产品数据中。如果它在那里,那么它将被正确转换为 JSON。

@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
{
    //... other codes
    productData.setMatcode("001100"); // add your logic to set this value
}

【讨论】:

  • 请注意,因为标题提到了 JSON。要将属性添加到您的 OCC Web 服务,您需要在 *commercewebservices-beans.xml 中将属性定义到 ProductWsDTO 并添加到 dto-level-mappings-v2-spring.xml 的映射。
  • 是的,但是这里 SO 试图将 ProductData 作为 Json Obj 放在店面
【解决方案2】:

如果你使用 hql,你可以这样做:

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

}

如果你想了解更多关于Transient注解的内容,请关注Transient Attribute

【讨论】:

    【解决方案3】:

    您可以使用像 gson 这样的库。 假设你有这样的模型:

    public class Products
    {
        private List<Product> products;
    }
    
    
    public class Product
    {
        private String name;
    
        private String desc;
    
        private String Quantity;
    }
    

    简单的方法:

    为产品模型添加另一个属性

      private String matcode;
    

    现在可以使用以下代码:

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

    另一条更长的路:

    一个。读取 JSON 响应 湾。转换为对象(你应该已经在做) C。使用 gson 将对象转换为 JsonElement 作为数组 d。根据需要迭代和更新 JsonObject e.将更新后的 JsonElement 转换为字符串输出。

    下面的工作代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多