我正在扩展JsonSerializer以覆盖它的序列化方法,如下所示:
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();
}
}
用户定义如下:
@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;
}
}
我正在调用以下控制器方法,以便它返回用户:
@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);
}
但它返回分页内容,如:
{
"_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
}
}
如您所见,返回的数据位于“内容”键中。我不希望这种情况发生,我怎样才能使数据显示嵌入“用户”键? (这是未覆盖serialize方法时的默认值)