【发布时间】:2017-03-20 22:52:54
【问题描述】:
我有一个内联一些关系字段的弹簧数据投影。应用投影时,日期时间字段不再输出为 iso8601(就像没有投影一样),而是以另一种格式输出。
如何将我的投影格式设置为 ISO8601 的日期时间?这是我目前的预测:
package io.cocept.model.projection;
import io.cocept.model.Meeting;
import io.cocept.model.User;
import org.springframework.data.rest.core.config.Projection;
import org.springframework.format.annotation.DateTimeFormat;
@Projection(name = "inlineUsers", types = { Meeting.class })
public interface MeetingInlineUsersProjection {
String getAddress();
String getDateTime();
String getMessage();
User getOwner();
User getInvitee();
}
还有我的会议课:
package io.cocept.model;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
@Entity
public class Meeting extends BaseEntity {
private User owner;
private User invitee;
private String address;
private Date dateTime;
private String message;
public Meeting() {
}
@ManyToOne
@NotNull
@JoinColumn(name = "owner_id")
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
@ManyToOne
@NotNull
@JoinColumn(name = "invitee_id")
public User getInvitee(){
return invitee;
}
public void setInvitee(User invitee){
this.invitee = invitee;
}
@NotNull
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@NotNull
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
public String getMessage(){ return message; }
public void setMessage(String message){ this.message = message; }
}
我尝试将装饰器 @DateTimeFormat(pattern = "YYYY") 添加到 getDateTime() 属性,但它不会更改输出的日期格式。
有什么想法吗?
谢谢 最大
【问题讨论】:
-
很高兴看看 Dean Clark 的回答是否有效(我怀疑)。但是你可以使用jackson并创建一个组件并使用json反序列化(stackoverflow.com/a/38186623/3003337)。
-
为什么在投影中使用String作为数据类型?
标签: java spring datetime spring-data spring-data-jpa