spring - Passing of list of type Baseclass between Webservices involving generics and conversion of polymorphic types -
i have 2 rest services using spring boot running on 2 different servers. using rest template communication.
there models shared these 2 services. these models of type 'idatatotransferred' .
'idatatotransferred' marker interface implemented various model beans.
i need write common logic passing list of these models between these rest services.
hence wrote logic uses parameters list<? extends idatatotransferred>
sender service receiver service.
update: code
idatatotransferred.java marker interface
datatobesent.java
datatobesent implements idatatotransferred{ //simple pojo }
senderservice.java
senddata(list<? extends idatatotransferred> uploaddataobjectlist){ //some code rest template //resttemplate.postforentity }
idatatotransferred interface shared between communicating webservices.
datatobereceived.java
datatobereceived implements idatatotransferred{ //simple pojo }
receiverservice.java
receivedata(list<? extends idatatotransferred> uploaddataobjectlist){ //some code convert uploaddataobjectlist datatobereceived }
note in rest service getting 415 error. unsupported media type. when use same list<? extends idatatotransferred>
on receiver.
when changed list<? super idatatotransferred>
on receiver side, works, guessing because of producer extends consumer super rules.
but problem can't typecast idatatotransferred type on receiver side. inside list getting linkedhashmap, json got converted linked hashmap between these services.
how can datatobereceived class object in receiverservice?
for simplicity sake have removed controllers. assume have same signature services.
if had known better terms search, have found answer before posting. alas.
in case found answer in stackoverflow page here this blog ofcourse.
the examples abstract classes. have used interfaces.
as mentioned in link. introduced below annotation in marker interface idatatotransferred:
@jsontypeinfo( use = jsontypeinfo.id.name, include = jsontypeinfo.as.property, property = "type") @jsonsubtypes({ @type(value = datatobesent.class, name = "datatransfer")})
the property type introduced in bean datatobesent property. type param used information conversion implementing type interface type. 1 can use different variable 1 named "type". in jsonsubtypes annotation , mention classes implementing interface.
datatobesent implements idatatotransferred{ //simple pojo // properties getter , setter string type = "datatransfer"; //with getter , setter }
the same exercise needs implemented on receiver side also. hence, have annotation below:
@jsontypeinfo( use = jsontypeinfo.id.name, include = jsontypeinfo.as.property, property = "type") @jsonsubtypes({ @type(value = datatobereceived.class, name = "datatransfer")})
here, have datatobereceived class implementing idatatotransferred interface. ofcourse need add type property datatobereceived class below:
datatobereceived implements idatatotransferred{ //simple pojo // properties getter , setter string type = "datatransfer"; //with getter , setter }
hope helps.
Comments
Post a Comment