JSON 反序列化为 RestTemplate 枚举

Deserialization of JSON to enum from RestTemplate

正在尝试使用 GitHub API REST 调用以获取具有以下 2 类:

的用户电子邮件列表
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Objects;

/**
 * {@link GitHubEmail} class represents a description of an email address (and additional info about
 * that email address) as GitHub API stores it.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class GitHubEmail {

  /**
   * Email address.
   */
  private String email;

  /**
   * Primary flag; denotes if the email address is primary one user uses. This is the one to email
   * to with the most confidence.
   */
  private boolean primary;

  /**
   * Flag that denotes if the email address has been verified by the user process.
   */
  private boolean verified;

  /**
   * If the email address is visible to the public, private, or no info (i.e., null)
   */
  private GitHubEmailVisibility visiblity;

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public boolean isPrimary() {
    return primary;
  }

  public void setPrimary(boolean primary) {
    this.primary = primary;
  }

  public boolean isVerified() {
    return verified;
  }

  public void setVerified(boolean verified) {
    this.verified = verified;
  }

  public GitHubEmailVisibility getVisiblity() {
    return visiblity;
  }

  public void setVisiblity(GitHubEmailVisibility visiblity) {
    this.visiblity = visiblity;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    GitHubEmail that = (GitHubEmail) o;
    return primary == that.primary &&
        verified == that.verified &&
        Objects.equals(email, that.email) &&
        visiblity == that.visiblity;
  }

  @Override
  public int hashCode() {

    return Objects.hash(email, primary, verified, visiblity);
  }

  @Override
  public String toString() {
    return "GitHubEmail{" +
        "email='" + email + '\'' +
        ", primary=" + primary +
        ", verified=" + verified +
        ", visiblity=" + visiblity +
        '}';
  }
}

这是 GitHubEmailVisibility.java:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
 * GitHub email visibility options.
 */
public enum GitHubEmailVisibility {

  PRIVATE,

  PUBLIC;

  @JsonValue
  public String toJson() {
    return name().toLowerCase();
  }

  @JsonCreator
  public static GitHubEmailVisibility fromValue(String value) {
    System.out.println("Is this ever even called?");
    if ("private".equalsIgnoreCase(value)) {
      return PRIVATE;
    } else if ("public".equalsIgnoreCase(value)) {
      return PUBLIC;
    }

    return null;
  }
}

使用 Spring 的 RestTemplate,我调用:

GitHubEmail[] gitHubEmails = rest.getForObject("https://api.github.com/user/emails?access_token={oAuthToken}&type=all&per_page=1000&page=1",
        GitHubEmail[].class, oAuthToken);
    Arrays.stream(gitHubEmails).forEach(gitHubEmail -> logger.info("GitHub email: " + gitHubEmail));

然而,visibility 总是返回为 null,即使负载字符串显然具有返回数组中至少一封电子邮件的值。

此外,我发现我在 @JsonCreator 方法中的 System.out.print 语句从未被调用。

这是以下几行的输出(第一行,将响应序列化为简单字符串,只是为了验证 visibility 有时确实会返回):

String emails = rest.getForObject("https://api.github.com/user/emails?access_token={oAuthToken}&type=all&per_page=1000&page=1",
        String.class, oAuthToken);
logger.info("emails consumed as a string: " + emails);

GitHubEmail[] gitHubEmails = rest.getForObject("https://api.github.com/user/emails?access_token={oAuthToken}&type=all&per_page=1000&page=1",
        GitHubEmail[].class, oAuthToken);
Arrays.stream(gitHubEmails).forEach(gitHubEmail -> logger.info("GitHub email: " + gitHubEmail));

这是这 2 组语句的服务器日志。如您所见,字符串版本的 3 封电子邮件中的第一封以 visibility: private 列出。但是,反序列化为 Java 对象的 all 3 封电子邮件具有 visibility 作为 null:

2018-05-01 21:42:51.477  INFO 5828 --- [nio-8080-exec-6] j.p.w.controller.LoginSuccessController  : emails consumed as a string: [{"email":"...","primary":true,"verified":true,"visibility":"private"},{"email":"...","primary":false,"verified":true,"visibility":null},{"email":"...","primary":false,"verified":true,"visibility":null}]
2018-05-01 21:42:51.524  INFO 5828 --- [nio-8080-exec-6] j.p.w.controller.LoginSuccessController  : GitHub email: GitHubEmail{email='...', primary=true, verified=true, visiblity=null}
2018-05-01 21:42:51.524  INFO 5828 --- [nio-8080-exec-6] j.p.w.controller.LoginSuccessController  : GitHub email: GitHubEmail{email='...', primary=false, verified=true, visiblity=null}
2018-05-01 21:42:51.524  INFO 5828 --- [nio-8080-exec-6] j.p.w.controller.LoginSuccessController  : GitHub email: GitHubEmail{email='...', primary=false, verified=true, visiblity=null}

如何将其反序列化到此 Java POJO 模型中?

我正在使用 Spring Boot 2.0,它看起来依赖于 Jackson 2.9.4

我在 https://api.myjson.com/bins/1h6ngn

托管了一个示例 json

和使用相同的 类,它运行良好。

    public static void main(String[] args) throws JsonProcessingException {

    RestTemplate rest = new RestTemplate();
    GitHubEmail[] gitHubEmails = rest.getForObject("https://api.myjson.com/bins/1h6ngn", GitHubEmail[].class);
    Arrays.stream(gitHubEmails)
        .forEach(gitHubEmail -> System.out.println(("GitHub email: " + gitHubEmail)));
}

输出

Is this ever even called?
Is this ever even called?
GitHub email: GitHubEmail{email='hemantsonu20@gmail.com', primary=false, verified=true, visiblity=PRIVATE}
GitHub email: GitHubEmail{email='another@gmail.com', primary=true, verified=false, visiblity=PUBLIC}

只需更改您的休息时间

String gitHubEmails = rest.getForObject("https://api.github.com/user/emails?access_token={oAuthToken}&type=all&per_page=1000&page=1",
        String.class, oAuthToken);
System.out.println(gitHubEmails);

验证您是否在 json 中获得 "visiblity" 节点。