users@jersey.java.net

[Jersey] Jetty and Tomcat different behaviors when returning JSON array

From: Jason Erickson <jerickson_at_factorlab.com>
Date: Mon, 6 Dec 2010 13:38:33 -0800

I have a method (a few actually, but this will do for an example):

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Observation[] getObservations(
                        @QueryParam("startDate") Date startDate,
                        @QueryParam("endDate") Date endDate,
                        @QueryParam("observerId") Long observerId,
                        @QueryParam("observerName") String observerName,
                        @QueryParam("subjectId") Long subjectId,
                        @QueryParam("subjectName") String subjectName,
                        @QueryParam("phenomenonId") Long phenomenonId,
                        @QueryParam("page") @DefaultValue("0") int page,
                        @QueryParam("pageSize") @DefaultValue("25") int pageSize) {
                ObservationCriteria criteria = new ObservationCriteria();
                criteria.setStartDate(startDate);
                criteria.setEndDate(endDate);
                criteria.setPage(page);
                criteria.setPageSize(pageSize);
                criteria.setObserverId(observerId);
                criteria.setObserverName(observerName);
                criteria.setSubjectId(subjectId);
                criteria.setSubjectName(subjectName);
                criteria.setPhenomenonId(phenomenonId);
                List<Observation> obsResult = observationDao.getByCriteria(criteria);
                return obsResult.toArray(new Observation[] {});
        }

 
The data I get back is correct. However, when I deploy the application in jetty, the response is returned as a JSON object with one named field that contains the array like:
{"observation": [{"id":"440", "date":"2010-10-28"},{"id":"308", "date":"2010-10-28"}]}

When I deploy the same application to tomcat 6, the response is returned as a JSON array like:
[{"id":"440", "date":"2010-10-28"},{"id":"308", "date":"2010-10-28"}]

Is this a bug? Is there a workaround?