users@jaxb.java.net

Re: Error marshalling JAXB object

From: swtking <rajkal23_at_yahoo.com>
Date: Fri, 7 Aug 2009 12:23:14 -0700 (PDT)

Thanks for your response. Made changes to MyHashMapAdapter.java the format I
needed but still no luck.

I am trying to get output in this format.

<xml>
<abc>
        <123></123>
        <345></345>
</abc>
<def>
        <789></789>
</def>
</xml>


MyBean.java

       private Map<String,List<String>> valueMapList=null;

       @XmlJavaTypeAdapter(MyHashMapAdapter.class)
       @XmlElement
       public Map<String, List<String>> getValueMapList() {
               return valueMapList;
       }

       public void setValueMapList(Map<String, List<String>> valueMapList) {
               this.valueMapList = valueMapList;
       }


MyHashMapAdapter.java

public class MyHashMapAdapter extends
XmlAdapter<HashMap<String,List<String>>, Map<String,List<String>>> {
       
       @Override
         public HashMap<String,List<String>> unmarshal( String[] value ) {
                                                        HashMap<String,List<String>> m = new HashMap<String,List<String>>();
                   return m;
          }


       @Override
       public String[] marshal(List<String> arg0) throws Exception {
               String[] s = (String[]) arg0.toArray();
               return s;
       }

}



Wolfgang Laun-2 wrote:
>
> The adapter converts between String[] and List<String> but the element to
> which the adapter is attached to is a Map<?,?>.
>
> You could attach an adapter to MyBean, to convert between its field
> valueMapList and whatever. You'll have to design a class that's capable of
> storing the same information as MyBean but can be handled by JAXB and is
> structured according to the XML structure you want.
>
> -W
>
>
> On Thu, Aug 6, 2009 at 9:16 PM, swtking <rajkal23_at_yahoo.com> wrote:
>
>>
>> Could anyone tell me what I am doing wrong.
>>
>> Trying to marshall a complex map (Map<String,List<String>>)
>>
>> MyBean.java
>>
>> private Map<String,List<String>> valueMapList=null;
>>
>> @XmlJavaTypeAdapter(MyHashMapAdapter.class)
>> @XmlElement
>> public Map<String, List<String>> getValueMapList() {
>> return valueMapList;
>> }
>>
>> public void setValueMapList(Map<String, List<String>>
>> valueMapList)
>> {
>> this.valueMapList = valueMapList;
>> }
>>
>>
>> MyHashMapAdapter.java
>>
>> public class MyHashMapAdapter extends XmlAdapter<String[], List<String>>
>> {
>>
>> public List<String> unmarshal( String[] value ) {
>> List<String> r = new ArrayList<String>();
>> for( String s : value )
>> r.add(s);
>> return r;
>> }
>>
>> public String[] unmarshal( List<String> value ) {
>> return value.toArray(new String[value.size()]);
>> }
>>
>> @Override
>> public String[] marshal(List<String> arg0) throws Exception {
>> String[] s = (String[]) arg0.toArray();
>> return s;
>> }
>>
>> }
>>
>>
>> This is my error
>>
>> java.io.IOException: Error marshalling JAXB object of type "class
>> com.MyBean".
>> at
>>
>> com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:145)
>> at
>>
>> com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:254)
>> at
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:578)
>> at
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:502)
>> at
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:493)
>> Truncated. see log file for complete stacktrace
>> com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 3 counts of
>> IllegalAnnotationExceptions
>> Adapter com.MyHashMapAdapter is not applicable to the field type
>> java.util.Map<java.lang.String, java.util.List<java.lang.String>>.
>> this problem is related to the following location:
>> at
>> @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(type=class
>> javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter$DEFAULT,
>> value=class
>> com.MyHashMapAdapter)
>> at public java.util.Map com.MyBean.getValueMapList()
>> at com.MyBean
>> java.util.Map is an interface, and JAXB can't handle interfaces.
>> this problem is related to the following location:
>> at java.util.Map
>> at public java.util.Map com.MyBean.getValueMapList()
>> at com.MyBean
>> java.util.Map does not have a no-arg default constructor.
>> this problem is related to the following location:
>> at java.util.Map
>> at public java.util.Map com.MyBean.getValueMapList()
>> at com.MyBean
>>
>> at
>>
>> com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
>> at
>>
>> com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:447)
>> at
>>
>> com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:288)
>> at
>>
>> com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1111)
>> at
>> com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
>> Truncated. see log file for complete stacktrace
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Error-marshalling-JAXB-object-tp24852727p24852727.html
>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>
>

-- 
View this message in context: http://www.nabble.com/Error-marshalling-JAXB-object-tp24852727p24870320.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.