【问题标题】:JSON array within an object is not being parsed对象内的 JSON 数组未被解析
【发布时间】:2020-02-25 15:53:29
【问题描述】:

我正在使用 hibernate、spring 和 fastxml/Jackson 创建基于 JSON 的休息服务。

我正在发送以下 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
        }
    ]
}

要解析成以下类:

@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() {

    }
}

我将该 JSON 发送到以下 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;
}

它很好地解析了 PartsRequest 对象,但将partsRequestLines 列表设置为空。谁能告诉我如何让 Jackson/REST 正确解析子对象列表?

【问题讨论】:

    标签: json hibernate spring-mvc


    【解决方案1】:

    我想通了。我在 API 上的方法签名缺少 @RequestBody 注释。

    【讨论】:

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