简体   繁体   中英

How to use HashMap as a parameter in Web service

I am trying to make a dynamic Web Service in which i will be expecting a Java hash map or an Array list for the argument.

I am using the following code in Class Code:

package demo;

import java.util.ArrayList;

import javax.jws.WebService;

@WebService
public class HashMapTest {
    public HashMapTest() {
        super();
    }

    public int getResponse(ArrayList<String> hm) {
        return hm.size();
    }
}

I am using an IDE: Oracle Jdeveloper 11g. when i use the Wizard in the same, the output WSDL is as given below:

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
     name="HashMapTestService"
     targetNamespace="http://demo/"
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
     xmlns:tns="http://demo/"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
     xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
    >
    <wsdl:types>
    </wsdl:types>
    <wsdl:portType name="HashMapTest">
    </wsdl:portType>
    <wsdl:binding name="HashMapTestSoapHttp" type="tns:HashMapTest">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    </wsdl:binding>
    <wsdl:service name="HashMapTestService">
        <wsdl:port name="HashMapTestPort" binding="tns:HashMapTestSoapHttp">
            <soap:address location="http://localhost:7101/DemoServer-Demo-context-root/HashMapTestPort"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

As easily seen, the WSDL is corrupt and cannot be used.

Is it just a bug in Jdeveloper or can we simply not use Collections API in Web service as a parameter?

Please help

this is caused by bug in JAXB . Use the following Code:

public class DTOObject
{ 
        HashMap hm = new HashMap();

    public void setHm(HashMap hm) {
        this.hm = hm;
    }

    public HashMap getHm() {
        return hm;
    }

    public int size() {
        return hm.size();
    }
}

and

public class HashMapTest {
    public HashMapTest() {
        super();
    }

    public int getResponse(Wrapped hm) {

        System.out.println(hm);
        return hm.size();
    }


}

It will solve the issue and create the wsdl correctly.

You can not use the Technology dependent type in Web Service . You should use String or Byte . If you want to pass collection as a argument then serialize it and pass it as argument in the form of bytes. At the other end create the instance of collection form bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM