users@jersey.java.net

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

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 14 Dec 2010 17:13:11 +0100

Hi Jason,

Do you use a special JSON configuration setting in your application?
Could you share some details on it?

The most helpful would be to provide a simple reproducible test case.
Ideally a maven project.

Thanks,

~Jakub

On 12/06/2010 10:38 PM, Jason Erickson wrote:
> 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") intpage,
> @QueryParam("pageSize") @DefaultValue("25") intpageSize) {
> 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?