Problema com jackson.databind

1 resposta
java
A

Boa noite a todos,

Tenho um json que estou tentando recuperar algumas tags:

{“notificationType”:“Bounce”,“bounce”:{“bounceType”:“Permanent”,“bounceSubType”:“Suppressed”,“bouncedRecipients”:[{“emailAddress”:“[email removido]”,“action”:“failed”,“status”:“5.1.1”,“diagnosticCode”:"Amazon SES has suppressed sending to this address because it has a recent history of bouncing as an invalid address. For more information about how to remove an address from the suppression list, see the Amazon SES Developer Guide: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/remove-from-suppressionlist.html "}],“timestamp”:“2020-03-22T12:48:46.997Z”,“feedbackId”:“010001710249db53-1742f91f-40c4-4426-bb8a-1ad27cbc4823-000000”,“reportingMTA”:“dns; amazonses.com”},“mail”:{“timestamp”:“2020-03-22T12:48:46.997Z”,“source”:“[email removido]”,“sourceArn”:“arn:aws:ses:us-east-1:019128763192:identity/[email removido]”,“sourceIp”:“152.250.204.219”,“sendingAccountId”:“019128763192”,“messageId”:“010001710249da16-a642b7e4-45fa-4702-bd05-692f49bbdce9-000000”,“destination”:[“[email removido]”]}}

JsonNode X = new ObjectMapper().readTree(SOURCE_JSON);

System.out.println("X= " + X.get(notificationType).asText());

System.out.println("X= " + X.get(bounce).asText());

notificationType, recebo normalmente “Bounce”
mas a segunda tag “bounce” esta retornando null…

Alguem tem alguma ideia?

1 Resposta

Villagram

Bom dia, eu costumo usar uma api do google para fazer parse de objetos em json. Segue exemplo:

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>

public static void main(String[] args) {
    String json = "{\"notificationType\":\"Bounce\"," +
            "\"bounce\":{" +
            "\"bounceType\":\"Permanent\"," +
            "\"bounceSubType\":\"Suppressed\"," +
            "\"bouncedRecipients\":" +
            "[{" +
            "\"emailAddress\":\"[email removido]\"," +
            "\"action\":\"failed\"," +
            "\"status\":\"5.1.1\"," +
            "\"diagnosticCode\":\"Amazon SES has suppressed sending to this address because it has a recent history of bouncing as an invalid address. For more information about how to remove an address from the suppression list, see the Amazon SES Developer Guide: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/remove-from-suppressionlist.html \"" +
            "}]," +
            "\"timestamp\":\"2020-03-22T12:48:46.997Z\"," +
            "\"feedbackId\":\"010001710249db53-1742f91f-40c4-4426-bb8a-1ad27cbc4823-000000\"," +
            "\"reportingMTA\":\"dns; amazonses.com\"" +
            "}," +
            "\"mail\":{" +
            "\"timestamp\":\"2020-03-22T12:48:46.997Z\"," +
            "\"source\":\"[email removido]\"," +
            "\"sourceArn\":\"arn:aws:ses:us-east-1:019128763192:identity/[email removido]\"," +
            "\"sourceIp\":\"152.250.204.219\"," +
            "\"sendingAccountId\":\"019128763192\"," +
            "\"messageId\":\"010001710249da16-a642b7e4-45fa-4702-bd05-692f49bbdce9-000000\"," +
            "\"destination\":[\"[email removido]\"]" +
            "}}";
    Gson gson = new Gson();
    Notification notification = gson.fromJson(json, Notification.class);
    System.out.println(ZonedDateTime.ofInstant(notification.getBounce().getTimestamp().toInstant(), ZoneId.of("GMT")));
    System.out.println(notification);
    System.out.println(json);
    System.out.println(gson.toJson(notification));
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Notification {

    private String notificationType;
    private Bounce bounce;
    private Mail mail;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bounce {

    private String bounceType;
    private String bounceSubType;
    private List<BouncedRecipients> bouncedRecipients;
    //yyyy-MM-dd'T'HH:mm:ss.S
    private Date timestamp;
    private String feedbackId;
    private String reportingMTA;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BouncedRecipients {

    private String emailAddress;
    private String action;
    private String status;
    private String diagnosticCode;

}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Mail {

    //yyyy-MM-dd'T'HH:mm:ss.S
    private Date timestamp;
    private String source;
    private String sourceArn;
    private String sourceIp;
    private String sendingAccountId;
    private String messageId;
    private List<String> destination;

}

Depois de feito o mapeamento do json em classes basta usar a biblioteca para fazer o parse.

Criado 25 de março de 2020
Ultima resposta 25 de mar. de 2020
Respostas 1
Participantes 2