ResourceProcessor不使用PagedResources

时间:2015-10-31 01:06:55

标签: spring-data spring-hateoas spring-rest

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

答案 0 :(得分:1)

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

  1. 您的资源处理器应该以元素类型为目标 - 因此请执行 implements ResourceProcessor<Resource<FileInfo>>

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

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

  4. 这应该会产生这样的结果:

    @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