【问题标题】:How to print JSON a date in correct format returned from a REST API?如何以从 REST API 返回的正确格式打印 JSON 日期?
【发布时间】:2015-01-13 11:51:51
【问题描述】:

我编写了一个从数据库获取数据的 REST API。我的表中有一个存储日期的列。我正在使用时间戳格式来存储日期。

我的问题是当我获取数据时,我无法以正确的格式显示日期。我收到的是 1420655400000 而不是 2015-01-08 00:00:00

这是我的控制器:

@RequestMapping(value="/getEstimation" , method=RequestMethod.GET)
public   List<Estimation1> getEstimation(ModelAndView model) throws IOException{

    List<Estimation1> estdetail;
    estdetail= estimation.getEstimationbyId(5);

    return estdetail;

}

getEstimationId(double)的实现:

@Override
    public List<Estimation1> getEstimationbyId(double id) {
        // TODO Auto-generated method stub
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  
     String sql = "SELECT * FROM estimation where est_id=" +id;
     List<Estimation1> estimdetails= jdbcTemplate.query(sql, new RowMapper<Estimation1>()

           {
                @Override
                public Estimation1 mapRow(ResultSet rs, int rowNum) throws SQLException

                {
                    Estimation1 aContact = new Estimation1();
                    aContact.setDate(rs.getTimestamp("est_date"));

                    aContact.setEst_contactperson(rs.getString("est_contact_person"));
                    aContact.setEst_customer(rs.getString("est_customer"));
                    aContact.setEst_revision(rs.getInt("est_revision"));
                    aContact.setEst_prjt(rs.getString("est_project"));
                    aContact.setEst_status(rs.getString("est_status"));

                    return aContact;
                }
            });
    return estimdetails;
}

这是我在执行后从数据库中获取的数据:

[{"date":1420655400000,"est_prjt":"project1","est_revision":0,"est_customer":null,"est_contactperson":"robert","est_status":null,"est_id":0.0,"ec":null}]**

我应该进行哪些更改才能以正确的格式打印日期?

【问题讨论】:

  • aContact 中的值的属性是什么?
  • @KayNelson 财产..??

标签: mysql json spring-mvc


【解决方案1】:

您需要向 Jackson 的对象映射器提示您希望将日期反序列化的格式。以下应该适合你

 @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
 private Timestamp date;

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多