Jackson deearlization:根上有两个键。我如何打开其中一个并忽略另一个?

2024-03-09

使用杰克逊 2.x

json 响应如下所示:

{
 "flag": true,
 "important": {
   "id": 123,
   "email": "[email protected] /cdn-cgi/l/email-protection"
 }
}

“flag”键不提供任何有用的信息。我想忽略“flag”键并将“important”值解包到“Important”的实例中。

public class Important {

    private Integer id;
    private String email;

    public Important(@JsonProperty("id") Integer id,
                     @JsonProperty("email") String email) {
        this.id = id;
        this.email = email;
    }

    public String getEmail() { this.email }

    public Integer getId() { this.id }
}

当我尝试将 @JsonRootName("important") 添加到重要并使用 DeserializationFeature.UNWRAP_ROOT_VALUE 配置 ObjectMapper 时,我收到 JsonMappingException:

根名称“标志”与类型的预期(“重要”)不匹配...

当我从 JSON 中删除“标志”键/值时,数据绑定工作得很好。如果我也将 @JsonIgnoreProperties("flag") 添加到“重要”,我会得到相同的结果。

UPDATES


updated class ... that will actually pass the compile step
@JsonRootName("important")
public static class Important {
    private Integer id;
    private String email;

    @JsonCreator
    public Important(@JsonProperty("id") Integer id,
                     @JsonProperty("email") String email) {
        this.id = id;
        this.email = email;
    }

    public String getEmail() { return this.email; }

    public Integer getId() { return this.id; }
}

实际测试:

@Test
public void deserializeImportant() throws IOException {
    ObjectMapper om = new ObjectMapper();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    Important important = om.readValue(getClass().getResourceAsStream("/important.json"), Important.class);

    assertEquals((Integer)123, important.getId());
    assertEquals("[email protected] /cdn-cgi/l/email-protection", important.getEmail());
}

results:

com.fasterxml.jackson.databind.JsonMappingException:根名称“flag”与类型[简单类型,类 TestImportant$Important] 的预期(“重要”)不匹配


由于 Jackson 中 JSON 解析的流式性质,恐怕没有简单的方法来处理此类情况。

从我的角度来看,使用某种包装器更容易做到。

考虑这段代码:

public static class ImportantWrapper {
    @JsonProperty("important")
    private Important important;

    public Important getImportant() {
        return important;
    }
}

以及实际测试:

@Test
public void deserializeImportant() throws IOException {
    ObjectMapper om = new ObjectMapper();
    //note: this has to be present
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Important important = om.readValue(getClass().getResourceAsStream("/important.json"), ImportantWrapper.class)
                                .getImportant();

    assertEquals((Integer)123, important.getId());
    assertEquals("[email protected] /cdn-cgi/l/email-protection", important.getEmail());
}

注意@JsonRootName("important")是多余的,在这种情况下可以删除。

这看起来有些丑陋,但只需相对较小的努力就可以完美地工作。这样的“包装器”也可以被泛化,但这更像是建筑的东西。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jackson deearlization:根上有两个键。我如何打开其中一个并忽略另一个? 的相关文章

  • 将数据传递到表单时的重定向后获取?

    我有几个场景 servlet 需要将数据从数据库检索到的记录传递到 JSP 中的表单 目前 我将此信息存储在请求中 使用 RequestDispatcher 转发到页面 一切都很好 然而 这不符合 PRG 模式 AFAIK 并且当然意味着刷
  • 何时在java中使用get/set方法[重复]

    这个问题在这里已经有答案了 我想知道何时在我的类中使用 get 和 set 方法 getName setName 以及何时简单classVariable name 反而 classVariable getName 这是使用 set 和 ge
  • Java 7 中的 Beans Binding 将被什么取代?

    我在某处读到 我忘记了链接 Beans Binding 将不会成为 Java 7 的一部分 有人知道什么会取代它吗 另外 当前版本的 Java 中是否有 Bean 绑定的替代方案 我建议JGoodies 绑定 https binding d
  • 使用 TLS 证书 JDBC 连接到 Oracle 数据库

    我正在尝试用 Java 编写一个连接类来使用 JDBC 驱动程序连接到 Oracle 数据库 但我想保护用于连接到 Oracle 数据库的参数 例如 jdbcurl 用户名 密码 我必须使用 TLS 证书概念来连接到 Java 中的 Ora
  • Java - 直观地拖动摆动元素

    有没有类似的解决方案http allen sauer com com allen sauer gwt dnd demo DragDropDemo DragDropDemo html PaletteExample http allen sau
  • “错误:无法找到或加载主类 org.apache.hadoop.util.RunJar”是什么意思?

    我正在尝试运行一个示例 因为它指出 Hadoop 实践 一书 http www manning com lam 第 15 页 这是需要运行的命令 bin hadoop jar hadoop examples jar 但我收到这个错误 Err
  • Spring 应用程序启动前的 Spring Boot 设置日志记录

    我有一个项目 在启动 SpringApplication 之前需要日志记录机制 我怎样才能做到这一点 我尝试设置自己的日志记录机制 LogManager getLogManager readConfiguration 但在 Spring 应
  • Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 给出时区作为 IST

    我有 SimpleDateFormat 构造函数作为 SimpleDateFormat yyyy MM dd T HH mm ss Z 我正在解析字符串 2013 09 29T18 46 19Z 我读到这里 Z 代表GMT UTC时区 但是
  • 如何修复XSS漏洞

    我们正在使用 fortify 扫描 java 源代码 它抱怨以下错误 Method abc sends unvalidated data to a web browser on line 200 which can result in th
  • 在 Graal.js 中使用 java 类

    使用 Graal js 如何将 java 类导入到 JS 脚本中 以下代码适用于 Nashorn JJS 但不适用于 Graal js 因为没有Java type 在graal中 我需要在某个时候调用truffle吗 var ArrayLi
  • Java:字符串连接和变量替换的最佳实践[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 在 Java 中连接字符串和添加变量值的方法有太多 我应该如何选择一个 优点 缺点 最佳用例等 MessageFormat forma
  • Spring Boot 中的外部化配置,多个应用程序在同一容器中运行

    我正在构建多个 Spring Boot 应用程序 这些应用程序将部署在同一个 servlet 容器上 但我很难让 Spring Boot 按照我想要的方式使用外部化配置文件 而不是像框架想要的那样 情况 多个 Spring Boot 应用程
  • 使 @Schedule 在集群环境中仅运行一次

    我有两个 tomee 实例集群 每个都有一个方法注释如下 Schedule dayOfWeek public void runMeDaily 我只想每天运行一次这个方法 每天不两次 每个实例一次 我可以使用此处描述的标志仅在一个WebLog
  • JavaFX:在 WebView img 标签中未加载本地图像

    以下是我的代码 一切安好 我可以加载远程页面 我可以放置 HTML 内容 但我的img标签显示一个X标志表示无法加载图像 Note 我的图像与类位于同一个包中JavaFX在 Smiley 文件夹中 我可以列出所有图像 这意味着路径没有问题
  • 如何使用 Java 到 TestRail 的 API 将测试用例添加到现有测试运行中?

    我在执行期间创建了一个测试运行 我想在它们开始执行的同时添加测试用例 如果测试用例尚不存在 则已创建 并且该测试用例应该与其他测试用例一起添加到现有的测试运行中 我尝试过使用setCaseIds在运行期间和更新运行之后 但这会覆盖现有的运行
  • javaFX,抛出 NullPointerException,位置是必需的

    我看过其他答案 但没有任何帮助我 抱歉 GUI新手只知道swing的基础知识 这是主课 package application import javafx application Application import javafx fxml
  • JVM 调试端口 7779 正在使用

    我正在使用 RAD 8 当我在调试模式下启动服务器时 它会显示一条错误消息 指出JVM debug port 7779 is in use 我多次遇到这个问题 因为我知道 RAD 使用了这个端口 所以我不得不停止这个过程窗口任务管理器 gt
  • 请解释*贪婪量词的工作原理

    Pattern ptn Pattern compile a Matcher mtch ptn matcher bbaac if mtch find System out println mtch group 输出 不打印任何内容 Patte
  • 数组所有可能的组合

    我有一个字符串数组 ted williams golden voice radio 我希望这些关键字的所有可能组合采用以下形式 ted williams golden voice radio ted williams ted golden
  • 为什么 JDOM 的 getChild() 方法返回 null?

    我正在做一个关于 html 文档操作的项目 我想要现有 html 文档中的正文内容将其修改为新的 html 现在我正在使用 JDOM 我想在我的编码中使用 body 元素 为此 我在编码中使用了 getChild body 但它向我的程序返

随机推荐