Spring MVC Test with RestTemplate: Generic collection fails (even with ParameterizedTypeReference) -
i working spring framework 4.3.1
i have following domain class
@xmlrootelement(name="persona") @xmltype(proporder = {"id","nombre","apellido","fecha"}) public class persona implements serializable { @xmlelement(name="id") @jsonproperty("id") public string getid() { return id; } ....
where each getter has @xmlelement
, @jsonproperty
annotations. working jaxb2
, jackson2
i have following too:
@xmlrootelement(name="collection") public class genericcollection<t> { private collection<t> collection; public genericcollection(){ } public genericcollection(collection<t> collection){ this.collection = collection; } @xmlelement(name="item") @jsonproperty("collection") public collection<t> getcollection() { return collection; } public void setcollection(collection<t> collection) { this.collection = collection; } @override public string tostring() { stringbuilder builder = new stringbuilder(); for(object object : collection){ builder.append("["); builder.append(object.tostring()); builder.append("]"); } return builder.tostring(); } }
about testing, many @test
s methods working through spring mvc test
work fine. @controller
, @restcontroller
work how expected.
note: can test crud scenarios, http methods
such post
, put
, get
, delete
. therefore able 1 entity , collection of entities.
note: previous note, works working around xml
, json
formats.
now trying testing through resttemplate
how kind of programmatic client, only fails collections. following:
@before public void setup(){ mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext).build(); resttemplate = new resttemplate(new mockmvcclienthttprequestfactory(mockmvc)); list<httpmessageconverter<?>> converters = new arraylist<>(); converters.add(httpmessageconverterconfig.marshallingmessageconverter()); converters.add(httpmessageconverterconfig.mappingjackson2httpmessageconverter()); resttemplate.setmessageconverters(converters); system.out.println("converters.size():" + converters.size()); }
i can confirm converters.size()
prints 2
the following xml , json
@test public void findallxmltest(){ requestentity<void> requestentity = restcontrollersupport_.createrequestentityforget(uri, retrieveuri); parameterizedtypereference<genericcollection<persona>> parameterizedtypereference = new parameterizedtypereference<genericcollection<persona>>(){}; responseentity<genericcollection<persona>> responseentity = resttemplate.exchange(requestentity, parameterizedtypereference); assertthat(responseentity, notnullvalue()); assertthat(responseentity.getstatuscode(), is(httpstatus.ok)); assertthat(responseentity.getheaders().getcontenttype(), is(mediatype.application_xml) ); assertthat(responseentity.getbody(), notnullvalue()); assertthat(responseentity.getbody().getclass(), is(genericcollection.class)); assertthat(responseentity.getbody().getcollection(), is(personas)); } @test public void findalljsontest(){ requestentity<void> requestentity = restcontrollersupport_.createrequestentityforget(uri, retrieveuri); parameterizedtypereference<genericcollection<persona>> parameterizedtypereference = new parameterizedtypereference<genericcollection<persona>>(){}; responseentity<genericcollection<persona>> responseentity = resttemplate.exchange(requestentity, parameterizedtypereference); assertthat(responseentity, notnullvalue()); assertthat(responseentity.getstatuscode(), is(httpstatus.ok)); assertthat(responseentity.getheaders().getcontenttype(), is(mediatype.application_json_utf8) ); assertthat(responseentity.getbody(), notnullvalue()); assertthat(responseentity.getbody().getclass(), is(genericcollection.class)); assertthat(responseentity.getbody().getcollection(), is(personas)); }
note: observe using parameterizedtypereference
both scenarios.
for json works.
but xml get:
org.springframework.web.client.restclientexception: not extract response: no suitable httpmessageconverter found response type [com.manuel.jordan.controller.support.genericcollection<com.manuel.jordan.domain.persona>] , content type [application/xml] @ org.springframework.web.client.httpmessageconverterextractor.extractdata(httpmessageconverterextractor.java:109)
what wrong or missing?
your problem use marshallinghttpmessageconverter
isn't generichttpmessageconverter
, expected parameterizedtypereference
in httpmessageconverterextractor
:
if (messageconverter instanceof generichttpmessageconverter) { generichttpmessageconverter<?> genericmessageconverter = (generichttpmessageconverter<?>) messageconverter; if (genericmessageconverter.canread(this.responsetype, null, contenttype)) {
the mappingjackson2httpmessageconverter
one.
so, suggest try jaxb2collectionhttpmessageconverter
.
Comments
Post a Comment