aspose file tools*
The moose likes Web Services and the fly likes @Produces JSON with return type List not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "@Produces JSON with return type List not working" Watch "@Produces JSON with return type List not working" New topic
Author

@Produces JSON with return type List not working

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all,

I have a RESTful class which has a method which returns list of employees. I am using Jersey RESTful implementation with glassfish. I annotated my method with

@Produces(MediaType.APPLICATION_JSON)

and I am returning a list of employees as List<Employee>

Then I get this error
Then I created a wrapperInstead of returning List<Employee> I am returning EmployeeList. This is working fine when there are more than 1 Employee objects in the list. If there is only one Employee object in the list I get this JSONIf there are no Employee objects then I get a null. If there are more than 1 Employee objects I get this JSONThe first JSON and this JSON is different. Here I get an array where as in the first JSON I get an object.

Can anyone please tell me how to receive array in the JSON irrespective of the number of Employee objects. (For 0 Objects empty array)

Thank you all in advance.


Love all, trust a few, do wrong to none.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I would try returning an array of Employee. The List interface is meaningless as a return type.

Bill
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

I did not understand. Can you please explain? Are you saying me to return an ArrayList instead of List?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
No, Bill is suggesting an array: Employee[]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Ulf Dittmer wrote:No, Bill is suggesting an array: Employee[]
What? An array? But why? I mean I want to know the advantage and how returning a List is bad. What if I want to return list of employees, their count, start index, end index for example.

Besides I have to even initialize the array and then start adding the employees. If its a List I need not bother about the initialization.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Generally speaking, clients of web services expect certain language independent data types, strings, integers, floating point, etc - plus repeating structures with these data types.

See for example, this discussion.

Language Independent is a key word here. When you say List in Java you are talking about a Java specific collection object implementing the List interface which has methods like add(), iterator(), contains() etc etc. All things that a client of your web service does not need or understand.

It seems to me that when you are thinking "list" you are really thinking array. It is very easy to get an array out of any List with the marvelous toArray() method.

Bill
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

It seems to me that when you are thinking "list" you are really thinking array
No I was speaking about java List not and array.

Okay I now understand why I should return an array instead of List.

But I have one question here. Even though I am returning a java List jersey converts it into json (key : value pair) and sends it right. At last the client will receive strings itself right. Then what is the problem in using List?
 
Don't get me started about those stupid light bulbs.
 
subject: @Produces JSON with return type List not working