【问题标题】:Hibernate removes "is" from fieldnameHibernate 从字段名中删除“is”
【发布时间】:2019-06-30 21:11:21
【问题描述】:

出于某种原因,hibernate(或 JpaRepository?)正在删除我在其余 api json 文档中的字段名的“is”部分。

例如,我的实体类中有字段“is_active”,在 json 响应中,该字段被重命名为 _active。

有没有办法禁用这种行为?

【问题讨论】:

  • 你为什么要把_放在你的字段名中? isActive 是 Java 命名约定。
  • 另外,“JSON 响应”的微小细节意味着它是 Jackson,而不是 Hibernate。
  • 没错,但是该字段被重命名为 Active。 "is" 仍然被过滤掉。
  • Json 文件?这与 Hibernate 或 JPA 有什么关系?你为什么不向我们展示实体代码以及你想用它做什么。

标签: java spring hibernate spring-data-jpa


【解决方案1】:

你需要@JsonProperty 指令:

@Test
void t() throws JsonProcessingException {
    C c = new C();
    String s = objectMapper.writeValueAsString(c);
    System.out.println(s);
}

public static class C {

    private boolean isActive = true;

    @JsonProperty("is_active")
    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        this.isActive = active;
    }
}

结果:

{
  "is_active" : true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 2021-03-08
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多