【问题标题】:ResourceProcessor is not working with PagedResourcesResourceProcessor 不使用 PagedResources
【发布时间】:2015-10-31 01:06:55
【问题描述】:

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

}

【问题讨论】:

  • spring-data-rest 在类路径上吗?

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


【解决方案1】:

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

  1. 您的资源处理器应该以元素类型为目标 - 所以实现 implements ResourceProcessor&lt;Resource&lt;FileInfo&gt;&gt;

  2. 要与 Spring Data Rest 集成,您的控制器不应是 @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

【讨论】:

  • 您好 Mathias,我已经尝试了您的解决方案,但仍然没有填充链接。
  • 你有 spring data Rest 在类路径中还是只是 spring hatoas
  • 我正在使用 spring boot,在我的 pom 中我有两个依赖项: org.springframework.bootspring-boot-starter-data-rest org.springframework.bootspring-boot-starter-hateoas
  • 看起来 spring-data-rest 没有为自定义控制器获取资源处理器 - 目前我不知道如何解决这个问题。
  • 我进行了一些研究并更新了我的答案 - 请看看我想出了什么。
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 2017-05-24
  • 2019-09-30
  • 1970-01-01
  • 2014-06-07
  • 2018-04-19
  • 1970-01-01
  • 2019-03-11
相关资源
最近更新 更多