您好我正在尝试将一些自定义链接添加到分页资源中,但没有成功。此问题可能与DATAREST-375有关,但有人可以验证我这样做是否正确。
@RestController
@RequestMapping(value = "/photos")
public class PhotoController implements ResourceProcessor<PagedResources<Resource<FileInfo>>> {
private static final String MEDIA = "media";
@Autowired
private FileSystemService photoService;
@RequestMapping(method = RequestMethod.GET)
public HttpEntity<PagedResources<Resource<FileInfo>>> getAllPhotos( Pageable pageable, PagedResourcesAssembler<FileInfo> asemb )
throws IOException {
Page<FileInfo> imagesInfo = photoService.getImagesInfo(pageable);
return new ResponseEntity<>( asemb.toResource(imagesInfo), HttpStatus.OK );
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<GridFsResource> getPhoto( @PathVariable("id") String id ) throws IOException {
GridFsResource imageByName = photoService.getImageById(id);
return new ResponseEntity<>( imageByName, HttpStatus.OK );
}
@Override
public PagedResources<Resource<FileInfo>> process(PagedResources<Resource<FileInfo>> resources) {
Collection<Resource<FileInfo>> content = resources.getContent();
for (Resource<FileInfo> resource : content) {
try {
resource.add(linkTo(methodOn(PhotoController.class).getPhoto(resource.getContent().get_id().toString())).withRel(MEDIA));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return resources;
}
}