如何在spring hibernate中反序列化日期格式?

时间:2016-01-13 08:29:02

标签: java hibernate rest spring-mvc

我正在写一个休息服务,我收到回复的格式为“1448994600000”,但我需要它以日期,月份,年份格式作出答复。

如果我通过给出12-2-2015格式发送数据,则会给我错误

  

客户端发送的请求在语法上不正确

作为回应我得到“12333333333”格式,我需要它以12-2-2015回复

我已经使用下面的代码对其进行反序列化,但是我的代码无法解决问题,请指导我。

 import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

import javax.persistence.Column;   
import javax.persistence.Entity;  
import javax.persistence.GeneratedValue;  
import javax.persistence.Id;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;  


@JsonAutoDetect
@Entity  
@Table(name = "DataValueTable")  
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})  
public class DataValueTable  implements Serializable {  

 private static final long serialVersionUID = 1L;  

 @Id  
 @GeneratedValue  
 @Column(name = "ID")  
 private long id;  

 @JsonSerialize(using=JsonDateSerializer.class)
 @Column(name = "Time")  
 private Date time;  

public Date getTime() {
    return time;
}

public void setTime(Date time) {
    this.time = time;
}

JsonDateSerializer.java

package com.beingjavaguys.model;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
/**
 * Used to serialize Java.util.Date, which is not a common JSON
 * type, so we have to create a custom serialize method;.
 *
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
我控制器中的

方法

 /* Getting List of objects in Json format in Spring Restful Services */  
 @RequestMapping(value = "/list", method = RequestMethod.GET)  
 public @ResponseBody  List getDatalist() {  


  List DataList = null;  
  try {  

      DataList = dataServices.getDataEntityList();  

  } catch (Exception e) {  
   e.printStackTrace();  
  }  

  return DataList;  
 }  

更新数据

@RequestMapping(value = "/updateData", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)  
     public @ResponseBody  
     Status updateData(@RequestBody DataValueTable dataObject) {  
      try {  
    //response.addHeader("Access-Control-Allow-Origin","*");
       dataServices.insertData(dataObject);  
       return new Status(1, "Data updated Successfully !");  
      } catch (Exception e) {  
       // e.printStackTrace();  
       return new Status(0, e.toString());  
      }  
      }  

1 个答案:

答案 0 :(得分:1)

您可以使用已引入java8的实现 导入特定库

import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter

LocalDate localDate= LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String stamp = localDate.format(formatter);

如果您需要从Date转换,则可以按照以下方式进行转换

Date input = new Date();
LocalDate localDate = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

输出可能像13-01-2016