Hello,

 I was struggling to return a hashmap from a cxf based web service and I
came across your tutorial. I tried the same way, but still could not suceed
in that.I have presented my issue below. If found time, pls have a look at
this and suggest me :)


My portion of WSDL is as follows which I want to be a hashmap

  <xsd:element name="LaunchVMsResponse">
           <xsd:complexType name = "resultMap">           
               <xsd:sequence>        
                        <xsd:element name="entry" minOccurs="0"
maxOccurs="unbounded">                                                          
        
                                <xsd:complexType>
                                        <xsd:sequence>
                                                <xsd:element name="key" 
type="xsd:string"/>
                                                <xsd:element name="value" 
type="xsd:string"/>
                                         </xsd:sequence>
                                </xsd:complexType>
                        </xsd:element>                                  
                </xsd:sequence>
             </xsd:complexType>          
         </xsd:element>

The coresponding service interface is as follows:-


@WebService(targetNamespace = "http://www.eads.net/ved/contract/manager";,
name = "VMManagerServicePortType")
@XmlSeeAlso({net.eads.ved.schema.manager.ObjectFactory.class,java.util.HashMap.class})
 
@XmlJavaTypeAdapter(ResultMapAdapter.class) 
public interface VMManagerServicePortType {

    @WebResult(name = "entry", targetNamespace = "")
    @RequestWrapper(localName = "LaunchVMs", targetNamespace =
"http://www.eads.net/ved/schema/manager";, className =
"net.eads.ved.schema.manager.LaunchVMs")
    @WebMethod(operationName = "LaunchVMs")
    @ResponseWrapper(localName = "LaunchVMsResponse", targetNamespace =
"http://www.eads.net/ved/schema/manager";, className =
"net.eads.ved.schema.manager.ResultMap")
    
    /*public java.util.List<net.eads.ved.schema.manager.ResultMap.Entry>
launchVMs(
        @WebParam(name = "hostList_launch", targetNamespace = "")
        java.util.List<java.lang.String> hostListLaunch
    );*/
   


    public HashMap<String,String> launchVMs(
            @WebParam(name = "hostList_launch", targetNamespace = "")
            java.util.List<java.lang.String> hostListLaunch
        );
}


I have hidden the auto generated method description and wrote my own. I have
also implemented one more class called ResultMapAdapter.class in which I
have doe marshalling and unmarshaling of hashmap to resultmap and vice
versa.

public class ResultMapAdapter extends
XmlAdapter<ResultMap,HashMap<String,String>>{

        @Override
        public ResultMap marshal(HashMap<String, String> v)  {

                try{
                System.out.println("burrr  marshal");
                ResultMap rMap = new ResultMap();
                ResultMap.Entry entry = new ResultMap.Entry();
                Iterator<String> i = v.keySet().iterator();
                while(i.hasNext())
                {
                        String temp = i.next();
                        entry.setKey(temp);
                        entry.setValue(v.get(temp));
                        rMap.getEntry().add(entry);
                }
                /*JAXBContext jc = JAXBContext.newInstance(HashMap.class);
                Marshaller m = jc.createMarshaller();
                m.marshal(new JAXBElement(new 
QName(""),HashMap.class,rMap),System.out);*/
                return rMap;
                }
                catch(Exception e){
                e.printStackTrace();
                return null;
                }
        }

        @Override
        public HashMap<String, String> unmarshal(ResultMap v)  {
                try{
                System.out.println("burrr  unmarshal");
                HashMap<String,String> map = new HashMap<String,String>();
                List<ResultMap.Entry> entries = new 
ArrayList<ResultMap.Entry>();
                for(int i = 0; i<entries.size();i++)
                {
                        
map.put(entries.get(i).getKey(),entries.get(i).getValue());
                }
                return map;
                }
                catch(Exception e){
                        e.printStackTrace();
                        return null;
                        }
        }

 
I could deploy this service onto container and see its publishing but when I
invoke the method through a client, an erros comes in the container saying,
java.util.HashMap cannot be casted to java.util.List. I am struggling here
for the past few days and could not come to a solution. For your reference I
have also given my ResultMap class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "resultMap", propOrder = {
    "entry"
})
public class ResultMap {
        @XmlJavaTypeAdapter(ResultMapAdapter.class);

    protected List<ResultMap.Entry> entries;

        
    public ResultMap(){} // empty constructor
    
    public List<ResultMap.Entry> getEntry() {
        if (entries == null) {
            entries = new ArrayList<ResultMap.Entry>();
        }
        return this.entries;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "key",
        "value"
    })
    public static class Entry {

        @XmlElement(required = true)
        protected String key;
        @XmlElement(required = true)
        protected String value;

        /**
         * Gets the value of the key property.
         * 
         * @return
         *     possible object is
         *     {...@link String }
         *     
         */
        public String getKey() {
            return key;
        }
             public void setKey(String value) {
            this.key = value;
        }
        public String getValue() {
            return value;
        }
        
        public void setValue(String value) {
            this.value = value;
        }

    }
    
   
}


Can you figure out from these input where I am doing wrong ? Thanks a lot,


With regards
Balachandar
-- 
View this message in context: 
http://old.nabble.com/hashMap-binding-tp28287798p28287798.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to