ResourceProcessor 不适用于 PagedResources

ResourceProcessor is not working with PagedResources

您好,我正在尝试将一些自定义链接添加到分页资源中,但没有成功。此问题可能与 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;
}

}

我试了一下,找到了解决这个问题的方法:

  1. 你的资源处理器应该以元素类型为目标 - 所以实现 implements ResourceProcessor<Resource<FileInfo>>

  2. 要与 spring 数据集成,您的控制器不应该是 @RestController,而是 @RepositoryRestController

  3. 如果您使用 RepositoryRestController,则需要自动装配 PagedResourcesAssembler 而不是将其作为方法参数传递

结果应该是这样的:

@RepositoryRestController
@RequestMapping(value = "/photos")
public class PhotoController implements ResourceProcessor<Resource<FileInfo>> {

private static final String MEDIA = "media";

@Autowired
private FileSystemService photoService;

@Autowired
private PagedResourcesAssembler<FileInfo> asemb;

@RequestMapping(method = RequestMethod.GET)
public HttpEntity<PagedResources<Resource<FileInfo>>> getAllPhotos( Pageable pageable ) 
        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(Resource<FileInfo> resource) {

    resource.add(linkTo(methodOn(PhotoController.class).getPhoto(resource.getContent().get_id().toString())).withRel(MEDIA));

    return resource;
}

我在与您的设置类似的设置中尝试过此方法并且有效。

文档的这一部分提供了更多详细信息: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_repositoryresthandlermapping