首页 > 解决方案 > JSON array within an object is not being parsed

问题描述

I'm using hibernate, spring, and fasterxml/Jackson to create JSON based rest services.

I'm sending in the following JSON:

{

    "allParts" : true,
    "aogLocation" : "LAS",
    "companyId" : "20",
    "controlNo" : "1",
    "controlSeq" : "1234",
    "dateNeeded" : "2020-02-24",
    "timeNeeded" : "800",
    "employeeId" : "bob",
    "inventoryLocation" : "LAS",
    "requestType" : "STOCK",
    "shippingAddress": "123 e. st. Las Vegas, NV 12345",
    "tailNo" : "abc",
    "partsRequestLines" : [
        {
            "location": "LAS",
            "lineNote" : "I need this part really bad",
            "requestedPartDescription" : "it makes a plane go.",
            "requestedPartNumber" : "abc-123",
            "requestedQuantity" : 10
        }
    ]
}

To be parsed into the following classes:

@Getter
@Setter
public class PartsRequestDto extends AbstractDto {

    private Boolean allParts;
    private String aogLocation;
    private Date chgdate;
    private Integer chgpage;
    private String chgprog;
    private String chgtype;
    private String chguser;
    private String companyId;
    private String controlNo;
    private Integer controlSeq;
    private Date dateNeeded;
    private String employeeId;
    private String inventoryLocation;
    private Date requestDate;
    private Integer requestId;
    private String requestType;
    private String shippingAddress;
    private Integer status;
    private Timestamp sysEnd;
    private Timestamp sysStart;
    private String tailNo;
    private String timeNeeded;
    private Timestamp transStart;

    private List<PartsRequestLineDto> partsRequestLines;

    public PartsRequestDto() {

    }

    public PartsRequestDto(Boolean allParts, String aogLocation, Date chgdate, Integer chgpage, String chgprog,
                           String chgtype, String chguser, String companyId, String controlNo, Integer controlSeq,
                           Date dateNeeded, String employeeId, String inventoryLocation, Date requestDate, Integer requestId,
                           String requestType, String shippingAddress, Integer status, Timestamp sysEnd, Timestamp sysStart,
                           String tailNo, String timeNeeded, Timestamp transStart) {
        this.allParts = allParts;
        this.aogLocation = aogLocation;
        this.chgdate = chgdate;
        this.chgpage = chgpage;
        this.chgprog = chgprog;
        this.chgtype = chgtype;
        this.chguser = chguser;
        this.companyId = companyId;
        this.controlNo = controlNo;
        this.controlSeq = controlSeq;
        this.dateNeeded = dateNeeded;
        this.employeeId = employeeId;
        this.inventoryLocation = inventoryLocation;
        this.requestDate = requestDate;
        this.requestId = requestId;
        this.requestType = requestType;
        this.shippingAddress = shippingAddress;
        this.status = status;
        this.sysEnd = sysEnd;
        this.sysStart = sysStart;
        this.tailNo = tailNo;
        this.timeNeeded = timeNeeded;
        this.transStart = transStart;
    }
}




@Getter
@Setter
@AllArgsConstructor
public class PartsRequestLineDto implements Serializable {
    private Integer id;
    private Integer reqId;
    private String location;

    private String requestType;
    private Date etaTimestamp;
    private Date neededTimestamp;
    private Date requestedTimestamp;
    private Integer status;
    private String tailNumber;
    private String lineNote;
    private String packingListId;
    private String requestedPartDescription;
    private String requestedPartNumber;
    private Integer requestedQuantity;

    public PartsRequestLineDto() {

    }
}

I send that JSON into the following REST API:

@RequestMapping(value = "/create", method = RequestMethod.POST)
public PartsRequestDto createPartsRequest(PartsRequestDto partsRequestDto) throws Exception {
    PartsRequest partsRequest = partsRequestService.constructPartsRequestFromDto(partsRequestDto);
    PartsRequestDto response = partsRequestService.createPartsRequest(partsRequest);
    return response;
}

It parses the PartsRequest object just fine, but sets the partsRequestLines list to null. Can someone tell me how to get Jackson/REST to parse the sub-object list correctly?

标签: jsonhibernatespring-mvc

解决方案


I figured it out. My method signature on the API was missing a @RequestBody annotation.


推荐阅读