I am using ObjectMapper to convert a list of objects to JSON, however the date field converts into key-value see below current output
current output:
{"startDateTime": {"year":2021,"monthValue":8,"dayOfMonth":24,"hour":20,"minute":5,"second":56,"nano":0,"month":"AUGUST","dayOfWeek":"TUESDAY","dayOfYear":236,"chronology":{"id":"ISO","calendarType":"iso8601"}}
Output expected is
{"startDateTime": "24-08-2021 17:56:16",
"endDateTime": "24-08-2021 17:57:00",
"userName": "Lakshman"}
My code:
ObjectMapper objectMapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
objectMapper.setDateFormat(df);
String jsonList = objectMapper.writeValueAsString(userList);
System.out.println("userlog =>> " + jsonList);
User Class
public class UserEventsEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@JsonIgnore
// @CreationTimestamp
@Column(name="start_date_time")
@JsonFormat(pattern="dd-MM-yyyy HH:mm:ss", timezone="Asia/Kolkata")
private LocalDateTime startDateTime;
@JsonIgnore
// @CreationTimestamp
@Column(name="end_date_time", nullable =true)
@JsonFormat(pattern="dd-MM-yyyy HH:mm:ss", timezone="Asia/Kolkata")
private LocalDateTime endDateTime;
@Column(length = 25, nullable = false)
private String userName;
@JsonFormat(pattern="dd-MM-yyyy HH:mm:ss", timezone="Asia/Kolkata")
public LocalDateTime getStartDateTime() {
return startDateTime;
}
public void setStartDateTime(LocalDateTime startDateTime) {
this.startDateTime = startDateTime;
}
@JsonFormat(pattern="dd-MM-yyyy HH:mm:ss", timezone="Asia/Kolkata")
public LocalDateTime getEndDateTime() {
return endDateTime;
}
public void setEndDateTime(LocalDateTime endDateTime) {
this.endDateTime = endDateTime;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}