我使用javaspringmvc和mysql数据库,并尝试使用restapi。
问题在于日期字段,这是我的模型:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "patient_mesure")
@JsonAutoDetect
public class PatientMesure {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "mesure_patient")
private float mesure_patient;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "mesure", nullable = false)
private Mesure mesure;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "patient", nullable = false)
private Personne patient;
@Column(name = "date")
private Date date;
@Column(name = "inserted", nullable = false)
private Date inserted;
@Column(name = "updated", nullable = false)
private Date updated;
// getters and setters
}
这是我的休息控制器:
import com.eheio.spring.models.PatientMesure;
import com.eheio.spring.models.Personne;
import com.eheio.spring.services.PatientMesureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
public class PatientMesureRestController {
private PatientMesureService patientMesureService;
@Autowired
public void setPatientMesureService(PatientMesureService patientMesureService) {
this.patientMesureService = patientMesureService;
}
@GetMapping(value = "/getAllMesures")
public List<PatientMesure> listAllUsers(HttpServletRequest httpServletRequest) {
HttpSession httpSession = httpServletRequest.getSession(true);
Personne personne = (Personne) httpSession.getAttribute("personne");
return patientMesureService.findMesuresByPatient(personne.getId());
}
}
我得到的结果是:
{ id: 1, date: 1527801252000, inserted: 1527801252000, mesure_patient: 50, updated: 1527801252000}
如何获得日期字段的正确表单?像这样的 2018-05-31 21:14:12
2条答案
按热度按时间lyr7nygr1#
设置一个自定义jsonserialize并将其格式化,simpledateformat如下:
首先创建jsondateserializer类:
然后在getter方法中:
另外,如果您使用的是java 8,那么datetimeformatter是线程安全的(正如@philippe marschall所指出的):
p4tfgftt2#
你可以像下面这样使用