第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Jackson 自動解析 Spring Boot Application 中的 JSON

如何使用 Jackson 自動解析 Spring Boot Application 中的 JSON

冉冉說 2022-07-20 10:35:51
我有json 文件 ,其中包含 json 對象作為屬性值:{ "name": "name", "json": {...}}我需要在RestController中自動獲取它并將其用作JPA+Hibernate中的實體。我的實體是:更新 -> 更多指定實體@Entity@Table(name = "collections")public class Collection {    @Id    private String name;    @Column(name = "cache_limit")    private int limit;    @Column(name = "cache_algorithm")    private String algorithm;    @Transient    private JsonNode schema;    @JsonIgnore    @Column(name ="json_schema")    private String jsonSchema;    public Collection() {    }    public String getJsonSchema() {        return schema.toString();    }    public void setJsonSchema(String jsonSchema) {        ObjectMapper mapper = new ObjectMapper();        try {            schema = mapper.readTree(jsonSchema);        } catch (IOException e) {            throw new RuntimeException("Parsing error -> String to JsonNode");        }    }   ..setters and getters for name limit algorithm schema..}當(dāng)我使用時,entityManager.persist(Collection)我將json_schema列為NULL我該如何解決?問題可能出在setJsonSchema ()中更新:public String getJsonSchema() {        return jsonSchema;    }    public void setJsonSchema(JsonNode schema) {        this.jsonSchema = schema.toString();    }這樣的 getter/setter 并不能解決問題
查看完整描述

3 回答

?
慕的地10843

TA貢獻1785條經(jīng)驗 獲得超8個贊

您可以將JsonNode json屬性定義為@TransientJPA 不會嘗試將其存儲在數(shù)據(jù)庫中。但是,jackson 應(yīng)該能夠?qū)⑺鼇砘剞D(zhuǎn)換為 Json。


然后您可以為 JPA 編寫 getter/setter 代碼,這樣您就可以從 JsonNode 來回轉(zhuǎn)換為 String。您定義了一個getJsonString轉(zhuǎn)換JsonNode json為String. 那個可以映射到表列,例如“json_string”,然后定義一個設(shè)置器,在其中接收String來自 JPA 的值并將其解析為可用于杰克遜的 JsonNode。


不要忘記添加@JsonIgnore,getJsonString這樣杰克遜就不會嘗試將 json 轉(zhuǎn)換為 jsonString。


@Entity

@Table(name = "cats")

public class Cat {


  private Long id;


  private String name;


  @Transient

  private JsonNode json;


  @Id

  @GeneratedValue(strategy = GenerationType.AUTO)

  public Long getId() {

    return id;

  }


  @Column(name ="name")

  public String getName() {

    return name;

  }


  public void setName(String name) {

    this.name = name;

  }


  public void setId(Long id) {

    this.id = id;

  }


  // Getter and setter for name


  @Transient

  public JsonNode getJson() {

    return json;

  }


  public void setJson(JsonNode json) {

    this.json = json;

  }



  @Column(name ="jsonString")

  public String getJsonString() {

    return this.json.toString();

  }


  public void setJsonString(String jsonString) {

    // parse from String to JsonNode object

    ObjectMapper mapper = new ObjectMapper();

    try {

      this.json = mapper.readTree(jsonString);

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

}


查看完整回答
反對 回復(fù) 2022-07-20
?
HUX布斯

TA貢獻1876條經(jīng)驗 獲得超6個贊

如本文所述,您不必手動創(chuàng)建自定義 Hibernate 類型,因為您可以使用以下依賴項通過 Maven Central 簡單地獲取它:


<dependency>

    <groupId>com.vladmihalcea</groupId>

    <artifactId>hibernate-types-52</artifactId>

    <version>${hibernate-types.version}</version> 

</dependency> 

有關(guān)更多信息,請查看Hibernate Types開源項目。


然后你可以簡單地聲明你類的新類型。


@TypeDef(

    name = "jsonb-node", 

    typeClass = JsonNodeBinaryType.class

)

實體映射將如下所示:


@Type(type = "json-node")

@Column(columnDefinition = "json")

private JsonNode json;


查看完整回答
反對 回復(fù) 2022-07-20
?
ABOUTYOU

TA貢獻1812條經(jīng)驗 獲得超5個贊

創(chuàng)建響應(yīng)而不將邏輯放入實體 setter/getter 中,不依賴第三方:


ObjectNode node = JsonNodeFactory.instance.objectNode();

node.put("name", cat.getName());

node.set("json", new ObjectMapper().readTree(cat.getJson()));

return Response.ok(node).build();


查看完整回答
反對 回復(fù) 2022-07-20
  • 3 回答
  • 0 關(guān)注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號