xml - Creating a JAX-RS object that contains collections and primatives as values of a map -


i have attempting create jax-rs service allows mixed values of primitives, maps, , lists values maps , lists. attributes in domain model cannot defined data coming different systems attributes exist , others not. service needs supports both xml , json. json structure needs similar listed below, without nesting of elements due wrapper classes. xml structure not set yet , more malleable.

i have been using resteasy , jackson far , have been shying away vendor specific annotations realize may unavoidable. these services apis not locked in , open other suggestions can accomplish goal. 1 requirement standard rest service.

example object represents article:

@xmlaccessortype(xmlaccesstype.field) @xmlrootelement public class article {      @xmlelement     private string title;      @xmlelement     private string creator;      @xmlelement     private map<string, object> attributes;      public map<string, object> getattributes() {         return attributes;     }      public void setattributes(map<string, object> attributes) {         this.attributes = attributes;     }      public string gettitle() {         return title;     }      public void settitle(string title) {         this.title = title;     } } 

given object expect json like:

{     "title": "some title",     "attributes":     {         "relevance": 0.93,         "rating": 4,     },     "actions":     [         "a",         "c"     ] } 

and xml like:

<article>     <title>some title</title>     <attributes>         <entry>             <key>relevance</key>             <value xsi:type="xs:double">0.93</value>         </entry>         <entry>             <key>rating</key>             <value xsi:type="xs:int">4</value>         </entry>         <entry>             <key>actions</key>             <value>                 <list xsi:type="xs:string">a</list>                 <list xsi:type="xs:string">c</list>           </value>         </entry>     </attributes> </article> 

the issues have encountered if value of map standard arraylist json processing works fine xml not contain information contents of array. xml looks following:

<article>     <title>some title</title>     <attributes>         <entry>             <key>relevance</key>             <value xsi:type="xs:double">0.93</value>         </entry>         <entry>             <key>rating</key>             <value xsi:type="xs:int">4</value>         </entry>         <entry>             <key>actions</key>             <value>    ----->     <arraylist/>           </value>         </entry>     </attributes> </article> 

to display contents of list when value of map list needs wrapped in object has jaxb annotations on it. doing adds level json when accessing attributes, undesired. if xmladaptor used unwrap nested object xml again returns value.

ideally want maps , lists typed object wrappers have jaxb annotations on them can added.

something hierarchy below:

root class

@xmlaccessortype(xmlaccesstype.none) @xmlseealso({jaxblist.class, jaxbmap.class, jaxbstring.class}) abstract public class jaxbobject<e> {      @xmlelement     abstract public e getvalue();     abstract public void setvalue(e value); } 

implementing classes

@xmlaccessortype(xmlaccesstype.none) @xmlseealso({jaxbobject.class}) public class jaxblist extends jaxbobject<arraylist<jaxbobject<?>>> {      private arraylist<jaxbobject<?>> value;      public arraylist<jaxbobject<?>> getvalue() {         return this.value;     }      public void setvalue(arraylist<jaxbobject<?>> value) {         this.value = value;     } }  @xmlaccessortype(xmlaccesstype.none) @xmlseealso({jaxbobject.class}) public class jaxbmap extends jaxbobject<hashmap<string, jaxbobject<?>>> {      private hashmap<string, jaxbobject<?>> value;      public hashmap<string, jaxbobject<?>> getvalue() {         return this.value;     }      public void setvalue(hashmap<string, jaxbobject<?>> value) {         this.value = value;     } }   @xmlaccessortype(xmlaccesstype.none) @xmltype final public class jaxbstring extends jaxbobject<string> {      private string value;      public string getvalue() {         return this.value;     }      public void setvalue(string value) {         this.value = value;     } }   @xmlaccessortype(xmlaccesstype.none) @xmltype final public class jaxbint extends jaxbobject<integer> {      private integer value;      public integer getvalue() {         return this.value;     }      public void setvalue(integer value) {         this.value = value;     } } 

i expect if getvalue() annotated @xmlvalue wrapper classes not added xml or json @xmlvalue not valid in location. did see post @xmlvalue being valid in location moxy move moxy need know attempting supported it.

couple of ideas:

  1. jackson can use jaxb annotations own: support may or may not enabled default jax-rs container, if not, enabling easy
  2. jackson can output xml xml module, https://github.com/fasterxml/jackson-dataformat-xml -- can use jackson and/or jaxb annotations

one thing note respect xml whereas json can sensible json map java objects, xml has own idiosyncracies, , may more difficult exact structure on both sides. if @ possible, focus on data passing, , figuring out object representation, , see kinds of xml representations can produced. realize may not option, when dealing xml stuff others produce. being able accept slight differences makes life lot easier, when dealing java lists , maps.

as jaxb annotations: keep in mind xml-specific, worry vendor-specific annotations can balanced concern using xml annotations on json (and/or other data types). ability avoid or minimize use of annotations approach; using standard naming conventions , default structures; is, trying avoid tinkering data format representation, , accepting default mappings.


Comments