Oleksandr H
Oleksandr H

Reputation: 3015

Customize Spring Data Rest projection

I have an entity with Date field:

@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;

I have a projection with the field:

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
String getMyTime();

This is my java code:

@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>);
    });
}

When I try to create a projection for this entity in java code, I receive following format in JSON:

"myTime": "2017-07-27"

How to fix it to receive dates in yyyy-MM-dd'T'HH:mm:ss.SSSZ format?

Upvotes: 1

Views: 1221

Answers (1)

Oleksandr H
Oleksandr H

Reputation: 3015

Finally, I have found a solution:

Just add annotation to your field in Projection interface;

import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
Date getMyDate();

Upvotes: 1

Related Questions