Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据投影中的自定义日期时间格式_Java_Spring_Datetime_Spring Data_Spring Data Jpa - Fatal编程技术网

Java Spring数据投影中的自定义日期时间格式

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

我有一个spring数据投影,它内联了一些关系字段。应用投影后,日期时间字段不再作为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; }

}
我尝试将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()