2

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

}

4

1 回答 1

1

我试了一下,找到了解决方法:

  1. 您的资源处理器应该针对元素类型 - 所以实现 implements ResourceProcessor<Resource<FileInfo>>

  2. 要与 spring data rest 集成,您的控制器不应该是一个,@RestController而是一个@RepositoryRestController

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

这应该会导致这样的结果:

@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

于 2015-10-31T10:49:47.930 回答