spring data - ResourceProcessor is not working with PagedResources -
hi i'm trying add custom links paged resources without success. issue may related datarest-375 can please verify i'm doing right.
@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(); (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; }
}
i tried around little , found way to this:
your resource processor should target element type - implement
implements resourceprocessor<resource<fileinfo>>
to integrate spring data rest controller should not
@restcontroller
@repositoryrestcontroller
if use repositoryrestcontroller need autowire
pagedresourcesassembler
instead if passing method argument
that should result in this:
@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; }
i tried in similar setup yours , worked.
this part of documentation gives little more details on this: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#_repositoryresthandlermapping
Comments
Post a Comment