I'm extending JsonSerializer to override it's serialize method like so:
public class UserSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("timestamp", user.getTimestamp());
jsonGenerator.writeStringField("caption", user.getCaption());
jsonGenerator.writeEndObject();
}
}
User is defined as such:
@JsonSerialize(using = UserSerializer.class)
@Document
public class User {
@NotNull
@Id
private String id;
@NotNull
private Date timestamp;
private String caption;
public Date getTimestamp() {
return timestamp;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
And I'm calling the following controller method, in order for it to return the users:
@RequestMapping(value = "/users", method = RequestMethod.GET)
public ResponseEntity<PagedResources<User>> findAll(Pageable pageable) {
Page<User> results = userService.findAll(pageable);
return new ResponseEntity(pagedAssembler.toResource(results), HttpStatus.OK);
}
But it returns the paged content like:
{
"_embedded" : {
"users" : [ {
"content" : {
"timestamp" : "2016-02-07T21:32:22.830+0000",
"caption" : "hi"
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/....."
}
},
"page" : {
"size" : 5,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
As you can see, the returned data is inside a "content" key. I don't want this to happen, how can I make the data appear embedded in the "users" key? (This is the default when not overriding the serialize method)