java - Marshall/Unmarshall Nested Map with JaxB -


there several previous questions around using jaxb marshall/unmarshall java.util.map, many of pointed example, works great:

http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html

however, can't jaxb able marshall/unmarshall instances of map if map not member of @xmlrootelement. example, here's root element class,

@xmlrootelement @xmlaccessortype(xmlaccesstype.field) public static class customer {      private myfield myfield      myfield getmyfield() {         return myfield     }      void setmyfield(myfield myfield) {         this.myfield = myfield     }  } 

the definition of it's field class:

@xmlaccessortype(xmlaccesstype.field) public static class myfield{      map<string, string> getsomemap() {         return somemap     }      void setsomemap(map<string, string> somemap) {         this.somemap = somemap     }      @xmlelement     private map<string, string> somemap = new hashmap<string, string>() } 

and code drive marshalling:

    jaxbcontext jc = jaxbcontext.newinstance(customer.class)      customer customer = new customer()     myfield myfield1 = new myfield()     myfield1.somemap.put("foo", "bar")     myfield1.somemap.put("baz", "qux")     customer.myfield =  myfield1      marshaller marshaller = jc.createmarshaller()     marshaller.setproperty(marshaller.jaxb_formatted_output, true)     marshaller.marshal(customer, system.out) 

this example results in:

java.util.map interface, , jaxb can't handle interfaces. java.util.map not have no-arg default constructor. 

i writing code in groovy rather java, don't think should make of difference.

i able encounter same behavior using jaxb creating testcontroller of type @restcontroller, using spring boot.

import org.springframework.web.bind.annotation.requestmapping import org.springframework.web.bind.annotation.restcontroller  @restcontroller @requestmapping(value = "test") class testcontroller {      @requestmapping(value = "findlist")     list findlist() {         ["test1", "test2", "test3"] arraylist<string>     }      @requestmapping(value = "findmap")     map findmap() {         ["t1":"test1", "t2":"test2", "t3":"test3"] hashmap<string,string>     }      @requestmapping(value = "")     string find(){         "test something"     } } 

with jaxb default implementation in springboot, reproduce issue /test/findlist correctly render xml, /test/findmap generate error described in initial posting.

for me, solution problem switch xml rendering library jackson (there others xstream well).

using gradle build file (build.gradle), add jackson dependencies, similar how if using maven:

'com.fasterxml.jackson.core:jackson-core:2.7.1', 'com.fasterxml.jackson.core:jackson-annotations:2.7.1', 'com.fasterxml.jackson.core:jackson-databind:2.7.1-1', 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.7.1', 'org.codehaus.woodstox:woodstox-core-asl:4.4.1',


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -