Java Spring数据投影中的自定义日期时间格式
我有一个spring数据投影,它内联了一些关系字段。应用投影后,日期时间字段不再作为iso8601输出(就像没有投影一样),而是以另一种格式输出 如何使投影格式成为ISO8601的日期时间?以下是我目前的预测:Java Spring数据投影中的自定义日期时间格式,java,spring,datetime,spring-data,spring-data-jpa,Java,Spring,Datetime,Spring Data,Spring Data Jpa,我有一个spring数据投影,它内联了一些关系字段。应用投影后,日期时间字段不再作为iso8601输出(就像没有投影一样),而是以另一种格式输出 如何使投影格式成为ISO8601的日期时间?以下是我目前的预测: package io.cocept.model.projection; import io.cocept.model.Meeting; import io.cocept.model.User; import org.springframework.data.rest.core.conf
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; }
}
我尝试将decorator@DateTimeFormat(pattern=“YYYY”)
添加到getDateTime()属性中,但它不会更改输出的日期格式
有什么想法吗
谢谢
Max尝试
@DateTimeFormat(pattern=“yyyy”)
而不是像Jens Schauder在他们的评论中指出的那样,我意外地将投影中getDateTime()的类型设置为字符串,而不是日期。这可能以与spring默认值(iso8601)不同的格式进行了隐式toString转换
当我将投影从:
String getDateTime()
致:
与应用程序的其他部分一样,它将日期格式化为ISO8601。看看Dean Clark的答案是否有效(我怀疑)。但是您可以使用jackson创建组件并使用json反序列化()。为什么在投影中使用String作为数据类型?
Date getDateTime()