java ee - Customize serialization of a list of JAXB objects to JSON? -
i planning serialize list of jaxb objects json response. below format getting. in below response seeing 1 more object in between "systeminfolist" showing array. instead want dependent_systems_infos should directly show array []. if there single system info response still should should show in array format. using jackson parser, cxf.
format getting:
{ "dependent_systems_infos":{ "systeminfolist":[ { "system_name":"pz_service", "system_type":"internal", "service_infos":[ { "service_name":"getpzattributes", "status":"down", "response_time_ms":50 } ] }, { "system_name":"oms", "system_type":"external", "service_infos":[ { "service_name":"createorder", "status":"up", "response_time_ms":2000 }, { "service_name":"cancelorder", "status":"up", "response_time_ms":500 } ] } ] } }
format need:
{ dependent_system_infos : [ { system_name : 'oms' system_type: 'external' services_infos: [ { service_name : 'createorder' status : 'up' response_time_ms : 2000 }, { service_name : 'cancelorder' status : 'up' response_time_ms : 2000 } ] }, { system_name : 'pz_service' system_type: 'internal' services_infos: [ { service_name : 'getpzattributes' status : 'up' response_time_ms : 2000 } ] } ] }
jaxb classes wrote:
@xmlrootelement(name = "dependent_systems_infos") @xmlaccessortype(xmlaccesstype.field) public class itinerarystatusresponse { private list<systeminfo> systeminfolist; @xmllist public list<systeminfo> getsysteminfolist() { return systeminfolist; } public void setsysteminfolist(list<systeminfo> systeminfolist) { this.systeminfolist = systeminfolist; } } @xmltype(proporder = { "systemname", "systemtype", "serviceinfolist" }) @xmlaccessortype(xmlaccesstype.field) public class systeminfo { @xmlelement(name = "system_name", required = true) protected systemname systemname; @xmlelement(name = "system_type", required = true) protected systemtype systemtype; @xmlelement(name = "service_infos", required = true) protected list<serviceinfo> serviceinfolist; }
it know how you're generating output, main issue serializing root object contains list when want serialize list itself. expect outputted list if itinerarystatusresponse
had other fields in it?
you can remove @xmlrootelement
annotation , mark list element named "dependent_systems_infos":
@xmlaccessortype(xmlaccesstype.field) public static class itinerarystatusresponse { private list<systeminfo> systeminfolist; @xmlelement(name = "dependent_systems_infos", required = true) public list<systeminfo> getsysteminfolist() { return systeminfolist; } public void setsysteminfolist(list<systeminfo> systeminfolist) { this.systeminfolist = systeminfolist; } }
if doing serialization yourself, approach drop itinerarystatusresponse
object entirely (since it's wrapper around list), , serialize list serializationfeature.wrap_root_value = true
, root name provide:
objectmapper mapper = new objectmapper(); mapper.configure(serializationfeature.wrap_root_value, true); annotationintrospector introspector = new jaxbannotationintrospector(typefactory.defaultinstance()); mapper.setannotationintrospector(introspector); objectwriter writer = mapper.writerwithdefaultprettyprinter().withrootname("dependent_systems_infos"); system.out.println(writer.writevalueasstring(systeminfolist));
both of these approaches provide desired output in testing jackson 2.2.
Comments
Post a Comment