自定义 Spring 数据剩余投影
Customize Spring Data Rest projection
我有一个带有 Date
字段的实体:
@NotNull
@Temporal(TemporalType.TIMESTAMP) // this annotation impacts on result
@JsonFormat(pattern = DATE_FORMAT)
@Column(name = "my_time", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Date myTime;
我有场的投影:
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
String getMyTime();
这是我的 java 代码:
@Autowired
private ProjectionFactory projectionFactory;
....
public PagedResources<Resource<MyEntityProjection>> transform(final Page<MyEntity> page) {
return assembler.toResource(page, entity -> {
final MyEntityProjection projected = projectionFactory.createProjection(MyEntityProjection.class, entity);
return new Resource<>(projected, <some links>);
});
}
当我尝试在 java 代码中为此实体创建投影时,我在 JSON 中收到以下格式:
"myTime": "2017-07-27"
如何修复它以接收 yyyy-MM-dd'T'HH:mm:ss.SSSZ
格式的日期?
终于找到了解决办法:
只需在投影界面中为您的字段添加注释即可;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
Date getMyDate();
我有一个带有 Date
字段的实体:
@NotNull
@Temporal(TemporalType.TIMESTAMP) // this annotation impacts on result
@JsonFormat(pattern = DATE_FORMAT)
@Column(name = "my_time", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Date myTime;
我有场的投影:
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
String getMyTime();
这是我的 java 代码:
@Autowired
private ProjectionFactory projectionFactory;
....
public PagedResources<Resource<MyEntityProjection>> transform(final Page<MyEntity> page) {
return assembler.toResource(page, entity -> {
final MyEntityProjection projected = projectionFactory.createProjection(MyEntityProjection.class, entity);
return new Resource<>(projected, <some links>);
});
}
当我尝试在 java 代码中为此实体创建投影时,我在 JSON 中收到以下格式:
"myTime": "2017-07-27"
如何修复它以接收 yyyy-MM-dd'T'HH:mm:ss.SSSZ
格式的日期?
终于找到了解决办法:
只需在投影界面中为您的字段添加注释即可;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
Date getMyDate();