Why a jax-rs service with return type Iterable<MyClass> works while Collection<MyClass> doesn't?

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
@Stateless
@LocalBean
@Path("test")
public class SimpleRestService {
    //Suppose people is initialized and contains actual people
    private ArrayList<Person> people;

    @GET
    @PATH("/people/iterable")
    public Iterable<Person> getPeople(){
        //This one works
        return people;
    }

    @GET
    @PATH("/people/collection")
    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?

Answers


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.


Need Your Help

C#/WPF: Idea how to display the last row of a ListView separately?

c# wpf listview

I've a ListView with about 10 GridViewColumn's and about 100 Lines/Row.