Skip to content
Advertisement

Unable to find data in DTO from JSON object – parsing error ZonedDateTime

I am trying to convert the JSON timestamp object into Java

enter image description here

I have researched converting a JSON string to java but unsure what I’m looking for. I can get it to work if the JSON uses an array but unfortunately it does not use this approach.

JSON Payload

{
  "type": "RFID-read",
  "event": {
    "id": "3892fec6-9246-4699-ba86-99ab1df369a9",
    "timestamp": "2020-11-19T15:01:11.391+0000",
    "deviceId": "FX9600FB2D21",
    "data": {
      "format": "epc",
      "id": "000000000000000000000115",
      "reads": 1,
      "rssi": -72,
      "antennaId": "1"
    }
  },
  "analytics": {
    "tenant": "73876942a20c12550f996b2152e5ca9e",
    "resourceId": "000000000000000000000115",
    "location": "FX9600FB2D21",
    "timestamp": "2020-11-19T15:01:11.391+0000",
    "meta": {
      "type": "inventory"
    }
  }
}

Event DTO

@Data
@Builder(toBuilder = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ZebraEventReadsDto {

private String id;
private ZonedDateTime timestamp;
@NotNull
@Size(min = 1, max = 100)
private String deviceId;
private String format;
ZebraDataReadsDto data;

Update DTO

    @Data
@Builder(toBuilder = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ZebraLocationUpdateDto {

    private String type;
    ZebraEventReadsDto event;

    public static List<LocationUpdateDom> toDomainModel(ZebraLocationUpdateDto zebraLocationUpdateDto) {
        List<LocationUpdateDom> locationUpdateDomList = new ArrayList<>();

        locationUpdateDomList.add(LocationUpdateDom.builder()
                .deviceName(zebraLocationUpdateDto.zebraEventReadsDto.getDeviceId())
                .dateTime(zebraLocationUpdateDto.zebraEventReadsDto.getTimestamp())
                              
                .tagId(zebraLocationUpdateDto.zebraEventReadsDto.data.getId())

                .latLng(Optional.empty())
                .build());

        return locationUpdateDomList;
    }

}

Advertisement

Answer

To resolving the parsing error I used the following annotation above my timestamp variable.

@JSONFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
private ZonedDateTime timestamp;

I can now return 200 OK

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement