Reputation: 27
I am trying to get all the columns from table and convert it to json. so trying as below
List<Map<String,Object>> map=jdbcTemplate.queryForList("select * from test");
So it it gives list of map having key as the columnName and value as the columnValue.
then I am using ObjectMapper
to convert the map to as json
String tableJson=new ObjectMapper().writeValueAsString(map);
The table is having a column createdDate
and its type is timestamp
.
But when the objectmapper is converting the map to json then timestamp is converted to long.
Please help how do I create a json without converting timestamp to long. It can be String date.
Upvotes: 0
Views: 1558
Reputation: 36
Add jackson-databind to your project
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
now you can control how to convert the field value, using
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
private Date createdDate;`
You can set the date format directly in ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSZ");
objectMapper.setDateFormat(df);
String tableJson=objectMapper.writeValueAsString(map);
Upvotes: 1