user2291144
user2291144

Reputation: 65

cannot return arraylist from web service

here is my code for web service

    package com.notification;

import java.util.ArrayList;
import java.util.List;

public class NotificationMessage {
 public List<String> message(){
  List<String> al = new ArrayList<String>();
  al.add("Meeting at 12");
  al.add("School at 10am on 6");
  al.add("Holiday on 8th");
  return al;
 }
}

I want to return an arraylist containing String objects

but it gives following error

Exception: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String Message: java.util.ArrayList cannot be cast to java.lang.String

Upvotes: 1

Views: 4111

Answers (1)

JDGuide
JDGuide

Reputation: 6525

I had same issue, I tried lot and finally succeed. As per my knowledge you can not return direct arraylist in web service,because ArrayList is a concept of Java only. You need to create a separate POJO class for the arraylist

POJO Class :-

public class ResponseDataArrayList {
    ArrayList list;
    public ArrayList getList() {
        return list;
    }

    public void setList(ArrayList list) {
        this.list = list;
    }   
}

Now you need a small changes in Implementation method.Just set your arraylist using setter method.Also change the return type of the method to POJO class type.These steps are most important.

public ResponseDataArrayList getProdList(){

        ArrayList al=new ArrayList();           
        al.add("Meeting at 12");
       al.add("School at 10am on 6");
       al.add("Holiday on 8th");

        ResponseDataArrayList objwrapper=new ResponseDataArrayList();
        objwrapper.setList(al);

        return objwrapper;
    }

Now change the WSDL file accordingly .If you are running the application using Eclipse then just right click on project -> Run on Server.

Hope is will help you.

Upvotes: 1

Related Questions