我有一个Java类,里面有2个List对象,我是Json序列化父类。
@JsonSerialize
public class RequestSalesJson {
@JsonProperty("nonUniqueSalesList")
private List<SalesDataJson> getNonUniqueSalesDataJson;
@JsonProperty("uniqueSalesList")
private List<SalesDataJson> uniqueSalesDataJson;
public List<SalesDataJson> getGetNonUniqueSalesDataJson() {
return getNonUniqueSalesDataJson;
}
public void setGetNonUniqueSalesDataJson(List<SalesDataJson> getNonUniqueSalesDataJson) {
this.getNonUniqueSalesDataJson = getNonUniqueSalesDataJson;
}
public List<SalesDataJson> getUniqueSalesDataJson() {
return uniqueSalesDataJson;
}
public void setUniqueSalesDataJson(List<SalesDataJson> uniqueSalesDataJson) {
this.uniqueSalesDataJson = uniqueSalesDataJson;
}
}
SalesReturnJson.java
@JsonSerialize
public class SalesReturnJson {
@JsonProperty("starttime")
private String startTime;
@JsonProperty("pn")
private String partNumber;
@JsonProperty("so")
private String SalesOrderNumber;
@JsonProperty("wo")
private String workOrderNumber;
@JsonProperty("loc")
//other variables declared..
}
Controller.java: -
@RequestMapping(value = "/addAllSalesData",method = RequestMethod.POST)
public void addAllSalesData(@RequestBody RequestSalesJson requestSalesJsons){
log.info("POST : '/addSalesData'");
try{
System.out.print("In Controller "+requestSalesJsons.getUniqueSalesDataJson());
//salesService.processSalesData(requestSalesJsons);
}
catch(Exception e){
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
此处的值将为空。
以下是我正在使用的json: -
{ "uniqueSalesJson": [{"SO":4000955,"Part Number":"000","Locator":255638,"Lot Number":"P01-2059139","Reservation Quantity":2,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000955,"Part Number":"000","Locator":255652,"Lot Number":"P01-2059140","Reservation Quantity":10,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}],"nonUniqueSalesJson":[{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PACKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PICKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}]}
答案 0 :(得分:1)
您的代码中存在一些问题,让我怀疑您的应用程序是否已编译。首先,将SalesReturnJson
类重命名为SalesDataJson
。
然后检查您的@JsonProperty
注释。此处的值必须与Json String中的属性键完全匹配。重构所有这些东西将引导您进入根实体类:
@JsonSerialize
public class RequestSalesJson {
@JsonProperty("nonUniqueSalesJson")
private List<SalesDataJson> nonUniqueSalesDataJson;
@JsonProperty("uniqueSalesJson")
private List<SalesDataJson> uniqueSalesDataJson;
...
}
和你的SalesDataJson
类(缺少映射器通过配置忽略的许多属性):
@JsonSerialize
public class SalesDataJson {
@JsonProperty("Start_Time")
private String startTime;
@JsonProperty("Part Number")
private String partNumber;
@JsonProperty("SO")
private String SalesOrderNumber;
}
此示例按预期使用com.fasterxml.jackson.databind.ObjectMapper
希望有所帮助!