现在,以下对象正在被写入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模型:
public class AsyncResponse {
@JsonProperty("statusCode")
public Integer statusCode = null;
@JsonProperty("body")
public Object body = null;
}
1条答案
按热度按时间laximzn51#
感谢@user3738870,这个方法奏效了:
第一个