rest - Exposing a paginated search link and its arguments -
i'm using latest of spring rest , hateoas trying expose link search endpoint.
here resource assembler:
@component public class adminresourceassembler extends resourceassemblersupport<admin, adminresource> { public adminresourceassembler() { super(admincontroller.class, adminresource.class); } public adminresource toresource(admin admin) { adminresource adminresource = createresourcewithid(admin.getid(), admin); beanutils.copyproperties(admin, adminresource); adminresource.add(linkto(admincontroller.class).slash(admin.getid()).slash("module").withrel("module")); return adminresource; } }
here end point controller:
@requestmapping(value = urimappingconstants.path_separator + urimappingconstants.search, method = requestmethod.get, produces = mediatype.application_json_value) @responsebody public responseentity<pagedresources<adminresource>> search(@requestparam(value = "searchterm", required = true) string searchterm, @pageabledefault pageable pageable, pagedresourcesassembler<admin> pagedresourcesassembler, uricomponentsbuilder builder) { httpheaders responseheaders = new httpheaders(); pageable pagerequest = buildpagerequest(pageable.getpagenumber(), pageable.getpagesize()); page<admin> searchedadmins = adminservice.search(searchterm, pagerequest); responseheaders.setlocation(builder.path(urimappingconstants.path_separator + urimappingconstants.admins + urimappingconstants.path_separator + "search").queryparam("searchterm", searchterm).queryparam("page", pageable.getpagenumber()).queryparam("size", pageable.getpagesize()).buildandexpand(searchterm).touri()); pagedresources<adminresource> adminpagedresources = pagedresourcesassembler.toresource(searchedadmins, adminresourceassembler); return new responseentity<pagedresources<adminresource>>(adminpagedresources, responseheaders, httpstatus.ok); } private pageable buildpagerequest(int pageindex, int pagesize) { sort sort = new sort(new sort.order(sort.direction.asc, "lastname"), new sort.order(sort.direction.asc, "firstname")); return new pagerequest(pageindex, pagesize, sort); }
first, i'm not sure if should call in buildpagerequest method , pass in original pageable search service.
the problem have 2 fold:
the published link in response missing searchterm parameter:
{"rel":"self","href":"http://localhost:8080/nitro-project-rest/admins/search{?page,size,sort}
i expect like:
{"rel":"self","href":"http://localhost:8080/nitro-project-rest/admins/search{?searchterm,page,size,sort}
but again, newbie don't know sure.
and controller fetches first page of 10 items, ignoring page number , size arguments give in request:
curl -h "accept:application/json" --user joethebouncer:mignet http://localhost:8080/nitro-project-rest/admins/search?searchterm=irstna&page=3&size=5
i guess i'm not far asolution, don't know exposed link should like.
any directions nice :-)
edit: added information
the pageable configuration:
@override public void addargumentresolvers(list<handlermethodargumentresolver> argumentresolvers) { pageablehandlermethodargumentresolver resolver = new pageablehandlermethodargumentresolver(); argumentresolvers.add(resolver); super.addargumentresolvers(argumentresolvers); }
the service wrap around repository:
public page<admin> search(string searchterm, pageable page) { return adminrepository.search(searchterm, page); }
which interface:
@query("select admin lower(a.firstname) lower(concat('%', :searchterm, '%')) or lower(a.lastname) lower(concat('%', :searchterm, '%')) or lower(a.email) lower(concat('%', :searchterm, '%')) or lower(a.login) lower(concat('%', :searchterm, '%')) order a.lastname asc, a.firstname asc") public
page search(@param("searchterm") string searchterm, pageable page);
the database h2 fronted jpa , console shows:
one can see offset missing...
select admin0_.id id1_1_, admin0_.version version2_1_, admin0_.email email3_1_, admin0_.firstname firstnam4_1_, admin0_.lastname lastname5_1_, admin0_.login login6_1_, admin0_.password password7_1_, admin0_.password_salt password8_1_, admin0_.post_login_url post_log9_1_ admin admin0_ lower(admin0_.firstname) lower(('%'||'irstn'||'%')) order admin0_.lastname asc, admin0_.firstname asc, admin0_.lastname asc, admin0_.firstname asc limit 10
kind regards,
stephane eybert
no idea buildpagerequest doing...but bet don't need , it's source of paging problems
regarding self link. pagedresourceassembler creates simple default one...but can build more complicated 1 , pass in third parameter of toresource method. build own link in of many ways. i'd suggest like:
linkto(methodon(somecontroller.class).search(searchterm, pageable, null, null))
another thing note self link should never templated bug in spring-hateoas (really dependency) see https://jira.spring.io/browse/datacmns-515
Comments
Post a Comment