Right now, the following object is being written to a JSONB column in a postges db:
{
"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"
}
I'd like the lastModified
property to be written as a string instead, so something like:
{
"id": "custom_ 6ef7181d-6afd-4632-b867-523c9d690af4",
"name": "bla",
"tenantId": "test_tenant",
"description": null,
"lastModified": "2022-11-15T19:02:30.912Z",
"caseSensitive": "SENSITIVE"
}
I have tried updating my DAO to use:
private final ObjectMapper objectMapper = new ObjectMapper()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
but it doesn't seem to do anything. Am I missing something else? Thanks
This is a snippet of my 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);
}
}
And the AsyncResponse model that I'm inserting into the DB:
public class AsyncResponse {
@JsonProperty("statusCode")
public Integer statusCode = null;
@JsonProperty("body")
public Object body = null;
}
Thanks @user3738870, this worked:
<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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.