Take the 2-minute tour ×

Say I have a simple class Person with name and last name as properties. Also suppose I have a simple rest service with get endpoints that return a list of people.

public class Person {
    // name and last name properties plus getters and setters

// Service
public class SimpleRestService {
    //Suppose people is initialized and contains actual people
    private ArrayList<Person> people;

    public Iterable<Person> getPeople(){
        //This one works
        return people;

    public Collection<Person> getPeople(){
        //This one fails. HTTP 500. Nothing on server.log ...
        return people;

The fisrt endpoin test/people/iterable will work fine while test/people/collection will fail with HTTP 500. Does any one have a clue on this?

share|improve this question
add comment

1 Answer

Jersey has a registry relating the MIME type in the @Produces with the Java type the method is declared to return and the handler that processes the object to produce the payload of the response. It has a handler that converts Iterable and List to a JSON Array. It also has a handler that converts Map to a JSON Object. The problem with the Java interface Collection is that there is no indication as to whether it should be represented as a sequential collection (JSON Array) or an unordered collection of named items (JSON Object). Thus there is no handler registered by default for converting Collection to application/json. This is what the message in the exception logged on your application server indicates.

share|improve this answer
I'm not quite sure about this. It has to do more with generics and TypeEreasure. I did tests, creating a Person class and the services above, but I was able to return both Iterable or Collection indifferently. Then I added some generics and it stopped working for both, Iterable and Collection. I tried to reproduce the exact same scenario when I was able to return an Iterable and not a Collection, but unfortunately I couldn't do so. Then... I don't know whether this is still a valid question or not since I cannot replicate it –  Jonathan Morales Vélez Mar 3 at 22:36
add comment

Your Answer


