java - Spring hateoas xml serialization for list of resources built with ResourceAssemblerSupport -
i trying support xml responses spring hateoas based application. json responses work fine xml single resource. problem starts list of resources. spring mvc controller cannot serialize list built of resourceassemblersupport derived class. controller throws "org.springframework.web.httpmediatypenotacceptableexception: not find acceptable representation" curl command
curl -k -i -h "accept:application/xml" -h "media-type:application/xml" -x http://127.0.0.1:8080/admin/roles*
my hateoas resource wrapper around entity class:
@xmlrootelement @xmlseealso(rolemodel.class) public class roleresource extends resourcesupport { public rolemodel role; }
the controller simple:
@requestmapping(method = requestmethod.get) public @responsebody httpentity<list<roleresource>> getallroles() throws objectaccessexception, objectnotfoundexception { list<rolemodel> resp = rolesmanagement.getroles(); return new responseentity<list<roleresource>>(roleresourceassembler.toresources(resp), httpstatus.ok); }
resource assembler class:
@configuration public class roleresourceassembler extends resourceassemblersupport<rolemodel, roleresource> { public roleresourceassembler() { super(rolesrestcontroller.class, roleresource.class); } @bean public roleresourceassembler roleresourceassembler(){ return new roleresourceassembler(); } @override public roleresource toresource(rolemodel role) { roleresource res = instantiateresource(role); res.role = role; try { res.add(linkto(methodon(rolesrestcontroller.class).getrole(role.getroleid())).withselfrel()); } catch (objectaccessexception | objectnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return res; } }
when avoid resourceassemblersupport , build resources manually this:
@xmlrootelement @xmlseealso(rolemodel.class) public class roleslist { private list<resource<rolemodel>> roles; ... } @requestmapping(method = requestmethod.get) public @responsebody httpentity<roleslist> getallroles() throws objectaccessexception, objectnotfoundexception { list<rolemodel> resp = rolesmanagement.getroles(); list<resource<rolemodel>> roles =new arraylist<>(); (rolemodel model: resp) { resource<rolemodel> res = new resource<rolemodel>(model); res.add(linkto(methodon(rolesrestcontroller.class).getrole(model.getroleid())).withselfrel()); roles.add(res); } roleslist list = new roleslist(); list.setroles(roles); return new responseentity<roleslist>(list, httpstatus.ok); }
xml serialization works. guess avoid using resource assembler , build resources manually, makes code not clean , modular. wonder if still possible use resourceassemblersupport resource builder , return list of resources xml
a resource assembler converting 1 pojo/entity/whatever 1 hateoas resource. trying convert list list.
if assembler was
public class roleresourceassembler extends resourceassemblersupport<list<rolemodel>, rolesresource> {
and rolesresource wanted...it should work.
however suggest @ using pagedresourceassembler takes page of "things" , uses assembler create page of resources. page can full collection or page in collection. here's simple 1 categories:
public httpentity<pagedresources<categoryresource>> categories( pagedresourcesassembler<category> pageassembler, @pageabledefault(size = 50, page = 0) pageable pageable ){ page<category> shopbycategories = categoryservice.findcategories(pageable); pagedresources<categoryresource> r = pageassembler.toresource(shopbycategories, this.categoryassembler); return new responseentity<pagedresources<categoryresource>>(r, httpstatus.ok); }
but..i've had problems jackson marshaling pagedresources xml...works fine json.
Comments
Post a Comment