簡體   English   中英

將 org.joda.time.DateTime 作為字符串而不是 object 寫入數據庫?

[英]Write org.joda.time.DateTime to DB as string instead of object?

現在,以下 object 正在寫入 postges 數據庫中的 JSONB 列:

{
  "id": "custom_ 6ef7181d-6afd-4632-b867-523c9d690af4",
  "name": "bla",
  "tenantId": "test_tenant",
  "description": null,
  "lastModified": {
    "era": 1,
    "year": 2022,
    "zone": {
      "id": "Europe/London",
      "fixed": false,
      "uncachedZone": {
        "id": "Europe/London",
        "fixed": false,
        "cachable": true
      }
    },
    "millis": 1668536796965,
    "afterNow": false,
    "equalNow": false,
    "weekyear": 2022,
    "beforeNow": true,
    "dayOfWeek": 2,
    "dayOfYear": 319,
    "hourOfDay": 18,
    "yearOfEra": 2022,
    "chronology": {
      "zone": {
        "id": "Europe/London",
        "fixed": false,
        "uncachedZone": {
          "id": "Europe/London",
          "fixed": false,
          "cachable": true
        }
      }
    },
    "dayOfMonth": 15,
    "millisOfDay": 66396965,
    "minuteOfDay": 1106,
    "monthOfYear": 11,
    "secondOfDay": 66396,
    "centuryOfEra": 20,
    "minuteOfHour": 26,
    "yearOfCentury": 22,
    "millisOfSecond": 965,
    "secondOfMinute": 36,
    "weekOfWeekyear": 46
  },
  "caseSensitive": "SENSITIVE"
}

我希望將lastModified屬性寫成字符串,例如:

{
  "id": "custom_ 6ef7181d-6afd-4632-b867-523c9d690af4",
  "name": "bla",
  "tenantId": "test_tenant",
  "description": null,
  "lastModified": "2022-11-15T19:02:30.912Z",
  "caseSensitive": "SENSITIVE"
}

我嘗試更新我的 DAO 以使用:

private final ObjectMapper objectMapper = new ObjectMapper()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .setDateFormat(new StdDateFormat().withColonInTimeZone(true));

但它似乎沒有做任何事情。 我還缺少其他東西嗎? 謝謝

這是我的 DAO 的一個片段:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.healthmarketscience.sqlbuilder.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

@Component
public class AsyncResponseDao
{

    private static final String INSERT_ASYNC_RESPONSE_SQL = String.format(
        "INSERT INTO %s (%s,%s,%s,%s) VALUES (?,?,?,? :: jsonb)",
        GrammarDbSpec.asyncResponseTable.table.getName(),
        GrammarDbSpec.asyncResponseTable.tenantId.getName(),
        GrammarDbSpec.asyncResponseTable.id.getName(),
        GrammarDbSpec.asyncResponseTable.statusCode.getName(),
        GrammarDbSpec.asyncResponseTable.body.getName(),
        GrammarDbSpec.asyncResponseTable.tenantId.getName(),
        GrammarDbSpec.asyncResponseTable.id.getName());

    private final ObjectMapper objectMapper = new ObjectMapper()
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .setDateFormat(new StdDateFormat().withColonInTimeZone(true));

    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void put(
        final String tenantId,
        final String id,
        final AsyncResponse asyncResponse)
    {

        // Convert AsyncResponse.body (java.lang.Object) to JSON before we insert it into the postgres JSONB column
        final String asyncResponseBodyJsonString = objectMapper.writeValueAsString(asyncResponse.getBody());
        
        jdbcTemplate.update(
            INSERT_ASYNC_RESPONSE_SQL,
            asyncResponse.getStatusCode(),
            asyncResponseBodyJsonString,
            tenantId,
            id);
    }
}

以及我插入數據庫的 AsyncResponse model:

public class AsyncResponse   {
  @JsonProperty("statusCode")
  public Integer statusCode = null;

  @JsonProperty("body")
  public Object body = null;
}

在此處輸入圖像描述

感謝@user3738870,這有效:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
</dependency>
private final ObjectMapper objectMapper = new ObjectMapper()
  .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
  .setDateFormat(new StdDateFormat().withColonInTimeZone(true))
  // We are using JodaModule to write lastModifiedDate (org.joda.time.DateTime) to the DB as a string like
  // "2022-11-15T19:02:30.912Z" rather than an object like {"era": 1, "year": 2022, "zone": ...}
  .registerModule(new JodaModule());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM