Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Produces JSON with return type List not working

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would try returning an array of Employee. The List interface is meaningless as a return type.

Bill
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not understand. Can you please explain? Are you saying me to return an ArrayList instead of List?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Bill is suggesting an array: Employee[]
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic