我有一个Java类,其中有2个列表对象,并且我是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"}]}

最佳答案

您的代码中有些问题让我怀疑您的应用程序可以编译。首先,将SalesReturnJson类重命名为SalesDataJson
然后检查您的@JsonProperty批注。此处的值必须与Json字符串中的属性键完全匹配。重构所有这些东西将引导您进入根实体类:

@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一起使用

希望有帮助!

09-11 17:59