Java 如何在prodctdatajson中填充自定义属性?
我正在尝试根据一些请求条件获取产品数据。当我使用转换器和填充器将productmodel转换为productdata时,我会得到所有的产品数据作为响应 在将productmodel数据转换和填充为productdata时,我尝试设置字符串值,但没有帮助Java 如何在prodctdatajson中填充自定义属性?,java,json,hybris,populate,converters,Java,Json,Hybris,Populate,Converters,我正在尝试根据一些请求条件获取产品数据。当我使用转换器和填充器将productmodel转换为productdata时,我会得到所有的产品数据作为响应 在将productmodel数据转换和填充为productdata时,我尝试设置字符串值,但没有帮助 { "products": [ { //Getting from Product Model "name" : "ABC" "desc" : "abcd
{
"products": [
{
//Getting from Product Model
"name" : "ABC"
"desc" : "abcde"
"Quantity": 2"
//Not from Product Model
"matcode" : "001100"
},
]
}
是否可以在同一响应中再设置一个字符串值(
string matcode=“ABC”
)?如果您使用的是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;
}
}
如果您想阅读更多有关
瞬态注释的信息,请遵循您可以使用类似gson的库。
假设您有这样的模型:
public class Products
{
private List<Product> products;
}
public class Product
{
private String name;
private String desc;
private String Quantity;
}
现在可以使用以下代码:
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));
另一条更长的路径:
a。阅读JSON响应
B转换为对象(您应该已经这样做了)
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);
理想情况下,如果您在ProductData中正确设置了matcode
(属性),它将反映在响应中
通过在您的*beans.xml
中声明ProducctData内的Decarematcode属性,类似于
<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
<!-- other attributes -->
<property name="matcode" type="java.util.Set<java.lang.String>"/>
</bean>
意思是说,您想在上面给出的现有json中再附加一个属性matcode
?@TayyabRazaq是的,但这在ProductModel中不存在。好的,如果它不在product model中,则没有问题,但您可以扩展product model。您是否得到响应http调用的product model?第二,您将如何设置matcode的值?@TayyabRazaq是的,我得到了productmodel数据属性的响应……我无法设置matcode,因此tats y希望获得有关此方面的帮助,如果可能的话,如何设置?请注意,因为标题中提到了JSON。要将该属性添加到OCC webservices,您需要在*commercewebservices-beans.xml中定义ProductWsDTO的属性,并添加到dto-level-mappings-v2-spring.xml.Yeah的映射,但这里SO试图在店面上以Json Obj的形式获取ProductData
<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
<!-- other attributes -->
<property name="matcode" type="java.util.Set<java.lang.String>"/>
</bean>
@Override
public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
{
//... other codes
productData.setMatcode("001100"); // add your logic to set this value
}