【发布时间】:2020-05-25 07:23:05
【问题描述】:
我开始学习 HATEOAS。作为回应,我想首先显示页面详细信息,然后是页面链接,然后是资源。不幸的是,一切都以相反的顺序显示。我怎么能把“_links {}”和“page {}”的数据放在Json响应的开头,放在“_embedded {}”数据之前。他们总是走到最后:(
我的 REST 控制器:
@RestController
@RequestMapping(value = "/api")
public class WebController {
private static final int DEFAULT_PAGE_NUMBER = 0;
private static final int DEFAULT_PAGE_SIZE = 5;
@Autowired
private AlbumRepository albumRepository;
@Autowired
private AlbumModelAssembler albumModelAssembler;
@GetMapping("/test")
public ResponseEntity<PagedModel<AlbumModel>> getAllAlbums(
@PageableDefault(page = DEFAULT_PAGE_NUMBER, size = DEFAULT_PAGE_SIZE) Pageable pageable,
PagedResourcesAssembler<AlbumEntity> pagedResourcesAssembler) {
Page<AlbumEntity> albumEntities = albumRepository.findAll(pageable);
Link selfLink = new Link(ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString());
PagedModel<AlbumModel> collModel = pagedResourcesAssembler.toModel(albumEntities, albumModelAssembler,selfLink);
return new ResponseEntity<>(collModel, HttpStatus.OK);
}
}
我收到以下回复:
{
"_embedded": {
"albums": [
{
"title": "Top Hits Vol 1",
"description": "Top hits vol 1. description",
"releaseDate": "10-03-1981"
},
{
"title": "Top Hits Vol 2",
"description": "Top hits vol 2. description",
"releaseDate": "10-03-1982"
},
{
"title": "Top Hits Vol 3",
"description": "Top hits vol 3. description",
"releaseDate": "10-03-1983"
},
{
"title": "Top Hits Vol 4",
"description": "Top hits vol 4. description",
"releaseDate": "10-03-1984"
},
{
"title": "Top Hits Vol 5",
"description": "Top hits vol 5. description",
"releaseDate": "10-03-1985"
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/api/test?page=0&size=5"
},
"self": {
"href": "http://localhost:8080/api/test"
},
"next": {
"href": "http://localhost:8080/api/test?page=1&size=5"
},
"last": {
"href": "http://localhost:8080/api/test?page=1&size=5"
}
},
"page": {
"size": 5,
"totalElements": 10,
"totalPages": 2,
"number": 0
}
}
【问题讨论】: