jersey - How can I get rid of the namespace prefix from MOXy json output? -
java 7, jersey 2.17 + jersey-media-moxy 2.17
all jaxb classes have same namespace. findresponse , apipeapipe classes in different package, package-info class both packages has same annotation:
@javax.xml.bind.annotation.xmlschema(namespace = "xmlapi_1.0", elementformdefault = javax.xml.bind.annotation.xmlnsform.qualified)
the findresponse class has @xmlelementroot,
@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { }) @xmlrootelement(name = "findresponse") public class findresponse {
while apipeapipe class
@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "apipe.apipe") public class apipeapipe extends vllvll {
the objectfactory findresponse:
public findresponse createfindresponse() { return new findresponse(); }
the objectfactory apipeapipe:
@xmlelementdecl(namespace = "xmlapi_1.0", name = "apipe.apipe") public jaxbelement<apipeapipe> createapipeapipe(apipeapipe value) { return new jaxbelement<apipeapipe>(new qname("xmlapi_1.0", "apipe.apipe"), apipeapipe.class, null, value); }
the custom moxyjsonconfigresolver:
@provider public class mdmmoxyjsonconfigresolver implements contextresolver<moxyjsonconfig> { private final moxyjsonconfig config; public mdmmoxyjsonconfigresolver() { final map<string, string> namespaceprefixmapper = new hashmap<string, string>(); namespaceprefixmapper.put(javax.xml.xmlconstants.w3c_xml_schema_instance_ns_uri, "xsi"); namespaceprefixmapper.put("xmlapi_1.0", ""); config = new moxyjsonconfig() .setnamespaceprefixmapper(namespaceprefixmapper) .setnamespaceseparator(':') .setattributeprefix("@") .setvaluewrapper("value") .property(jaxbcontextproperties.json_wrapper_as_array_name, true) .setformattedoutput(true) .setincluderoot(true) .setmarshalemptycollections(true); } @override public moxyjsonconfig getcontext(class<?> type) { return config; } }
json output:
{ "findresponse" : { "result" : { "ns0:lag.interface" : [ { "ns0:objectfullname" : "network:35.121.34.101:lag:interface-31", "ns0:selfalarmed" : true, "ns0:name" : "interface-31", "ns0:administrativestate" : "portinservice", "ns0:basemacaddress" : "00-00-00-00-00-00", "ns0:cleicode" : "n/a", "ns0:displayedname" : "lag 31", "ns0:equipmentcategory" : "port", "ns0:equipmentstate" : "equipmentoperationallydown", "ns0:hardwarefailurereason" : "n/a"}] }}}
i've set namespace prefix blank in moxyjsonconfig, not seem work. how can rid of namespace prefix, 1 prepended attributes?
i've added test check output in xml format:
the code:
@test public void testmoxyjaxb() throws exception { apipeapipe apipe = findservice.findbyofn(apipeapipe.class, "svc-mgr:service-96849"); findresponse findresponse = getfindresponse(arrays.aslist(apipe)); { javax.xml.bind.jaxbcontext jaxbcontext = javax.xml.bind.jaxbcontext.newinstance(getclassestobebound()); marshaller marshaller = jaxbcontext.createmarshaller(); system.out.println("javax.xml.bind.jaxbcontext output: "); marshaller.marshal(findresponse, system.out); system.out.println(""); } { map<string, object> properties = new hashmap<string, object>(); properties.put(jaxbcontextproperties.default_target_namespace, "xmlapi_1.0"); javax.xml.bind.jaxbcontext jaxbcontext = org.eclipse.persistence.jaxb.jaxbcontextfactory.createcontext(getclassestobebound(), properties); org.eclipse.persistence.jaxb.jaxbmarshaller marshaller = (org.eclipse.persistence.jaxb.jaxbmarshaller)jaxbcontext.createmarshaller(); map<string, object> nsprefix = new hashmap<string, object>(); // nsprefix.put(xmlconstants.w3c_xml_schema_instance_ns_uri, "xsi"); nsprefix.put("xmlapi_1.0", ""); marshaller.setproperty(marshallerproperties.namespace_prefix_mapper, nsprefix); system.out.println("org.eclipse.persistence.jaxb.jaxbcontextfactory output: "); marshaller.marshal(findresponse, system.out); system.out.println(""); } { system.out.println("org.springframework.oxm.jaxb.jaxb2marshaller output: "); result result = new stringresult(); jaxb.marshal(findresponse, result); system.out.println(result.tostring()); system.out.println(""); } }
the output:
javax.xml.bind.jaxbcontext output: <?xml version="1.0" encoding="utf-8" standalone="yes"?><findresponse xmlns="xmlapi_1.0"><result><apipe.apipe><objectfullname>svc-mgr:service-96849</objectfullname><selfalarmed>false</selfalarmed><name>service-96849</name><grouppointer></grouppointer><id>96849</id><serviceid>500</serviceid><subscriberpointer>subscriber:1</subscriberpointer><administrativestate>down</administrativestate><description>n/a</description><displayedname>apipe 500</displayedname><aggroperationalstate>down</aggroperationalstate><compositesvcid>0</compositesvcid><compositesvcpointer></compositesvcpointer><configurednumberofsites>0</configurednumberofsites><customername>default customer</customername><mtuinconsistent>false</mtuinconsistent><numberofcircuitsinconsistent>false</numberofcircuitsinconsistent><numberofconnector>0</numberofconnector><numberofinterfacesinconsistent>false</numberofinterfacesinconsistent><numberofsites>0</numberofsites><numberofsitesinconsistent>false</numberofsitesinconsistent><olcstate>maintenance</olcstate><operationalflags/><sasentityname>a-pipe service #500</sasentityname><subscriberid>1</subscriberid><vctype>atmvcc</vctype></apipe.apipe></result></findresponse> org.eclipse.persistence.jaxb.jaxbcontextfactory output: <?xml version="1.0" encoding="utf-8"?><findresponse xmlns="xmlapi_1.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"><result><apipe.apipe xmlns:ns0="xmlapi_1.0"><objectfullname>svc-mgr:service-96849</objectfullname><selfalarmed>false</selfalarmed><name>service-96849</name><grouppointer></grouppointer><id>96849</id><serviceid>500</serviceid><subscriberpointer>subscriber:1</subscriberpointer><administrativestate>down</administrativestate><description>n/a</description><displayedname>apipe 500</displayedname><aggroperationalstate>down</aggroperationalstate><compositesvcid>0</compositesvcid><compositesvcpointer></compositesvcpointer><configurednumberofsites>0</configurednumberofsites><customername>default customer</customername><mtuinconsistent>false</mtuinconsistent><numberofcircuitsinconsistent>false</numberofcircuitsinconsistent><numberofconnector>0</numberofconnector><numberofinterfacesinconsistent>false</numberofinterfacesinconsistent><numberofsites>0</numberofsites><numberofsitesinconsistent>false</numberofsitesinconsistent><olcstate>maintenance</olcstate><operationalflags/><sasentityname>a-pipe service #500</sasentityname><subscriberid>1</subscriberid><vctype>atmvcc</vctype></apipe.apipe></result></findresponse> org.springframework.oxm.jaxb.jaxb2marshaller output: <?xml version="1.0" encoding="utf-8" standalone="yes"?><findresponse xmlns="xmlapi_1.0"><result><apipe.apipe><objectfullname>svc-mgr:service-96849</objectfullname><selfalarmed>false</selfalarmed><name>service-96849</name><grouppointer></grouppointer><id>96849</id><serviceid>500</serviceid><subscriberpointer>subscriber:1</subscriberpointer><administrativestate>down</administrativestate><description>n/a</description><displayedname>apipe 500</displayedname><aggroperationalstate>down</aggroperationalstate><compositesvcid>0</compositesvcid><compositesvcpointer></compositesvcpointer><configurednumberofsites>0</configurednumberofsites><customername>default customer</customername><mtuinconsistent>false</mtuinconsistent><numberofcircuitsinconsistent>false</numberofcircuitsinconsistent><numberofconnector>0</numberofconnector><numberofinterfacesinconsistent>false</numberofinterfacesinconsistent><numberofsites>0</numberofsites><numberofsitesinconsistent>false</numberofsitesinconsistent><olcstate>maintenance</olcstate><operationalflags/><sasentityname>a-pipe service #500</sasentityname><subscriberid>1</subscriberid><vctype>atmvcc</vctype></apipe.apipe></result></findresponse>
there's additional namespace attribute on tag of org.eclipse.persistence.jaxb.jaxbmarshaller output. it's not there in other 2 marshaller's output. reason namespace prefix prepended attribute name of json output?
Comments
Post a Comment