我正在使用Objectmapper将从数据库接收的json转换为对象User.class,类结构如下
@XmlRootElement
public class User {
public User() {
super();
}
@XmlElement
private String username=null;
@XmlElement
private Feedbacks feedbacks=null;
//getter setter....
}
用户有一个反馈类的实例,反过来又有Arraylist<反馈和GT;
@XmlRootElement
public class Feedbacks {
public Feedbacks() {
}
@XmlElement
private ArrayList<Feedback> FeedbackList=new ArrayList<>();
public ArrayList<Feedback> getFeedbackList() {
return FeedbackList;
}
public void setFeedbackList(ArrayList<Feedback> FeedbackList) {
this.FeedbackList = FeedbackList;
}
}
和
public class Feedback {
private String feedback=null;
//private String timeStamp=null;
/*@JsonDeserialize(using = DateDeserializer.class); */
@JsonFormat(pattern = "MM/dd/yyyy HH:mm")
private Date feedbackDate;
public Feedback (){}
}
我从db检索的示例json是:
{
"userName":"Test",
"feedbacks":{
"feedbackTOList":[
{
"feedback":"Please select appropriate value..1",
"timeStamp":"03/01/2000 14:52"
},
{
"feedback":"Please select appropriate value..2",
"timeStamp":"03/01/2018 13:50"
},
{
"feedback":"Please select appropriate value..3",
"timeStamp":"02/01/2018 10:52"
}
]
}
}
Json根据日期反对转换和排序反馈列表:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");
mapper = new ObjectMapper();
mapper.setDateFormat(formatter);
userObject= mapper.readValue(jsonPayload, User.class);
Collections.sort(user.getFeedbacks().getFeedbackList(), new
Comparator<Feedback>() {
public int compare(Feedback f1, Feedback f2) {
if (f1.getTimeStamp() == null || f2.getTimeStamp() == null)
return 0;
return (f2.getTimeStamp().compareTo(f1.getTimeStamp()));
}
});
问题是当角度UI使用休息服务中的对象时,而不是日期(Wed Aug 01 16:20:00 EDT 2018])
,时间戳值将作为timeStamp=1533154800000
发送。
我的问题是如何以给定格式或至少日期对象发送字符串而不是时间戳?
到目前为止,我尝试@JsonFormat(pattern = "MM/dd/yyyy HH:mm")
,自定义日期反序列化器,但没有运气(通过在stackoverflow,网站上引用许多其他帖子),有人可以让我知道我在做什么错误吗?