• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

@Produces JSON with return type List not working

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not understand. Can you please explain? Are you saying me to return an ArrayList instead of List?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Bill is suggesting an array: Employee[]
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic