As an illustration, let's suppose I'm creating an HTTP REST API that behaves as follows:
Return a collection of all employees:
GET /employees/
[
{
"id": 1,
"name": "John",
"roles": [
{
"id": 20,
"responsibility": 1000
},
...
]
},
...
]
Return a representation of employee with id 1:
GET /employees/1/
{
"id": 1,
"Name": "John",
"roles": [
{
"id": 20,
"responsibility": 1000
},
...
]
}
Return a collection of the employee's roles:
GET /employees/1/roles
[
{
"id": 20,
"responsibility": 1000
},
...
]
Return a representation of role with id 20:
GET /employees/1/roles/20
{
"id": 20,
"responsibility": 1000
}
In this way we can traverse the tree of the employee representation. There are likely many other employee endpoints besides /roles.
Using Jersey I split this up into a root resource with several subresources, for example:
@Path("/employees")
public class EmployeesResource() {
...
@GET
@Path("/{employeeId}/roles")
public RolesResource getRoles(@PathParam("employeeId") long employeeId) {
return new RolesResource(employeeId);
}
}
This produces the desired JSON result when I GET /employees
and /employees/1
but when I GET /employees/1/roles
I get the response body:
{
"roles": [
{
"id": 20,
"responsibility": 1000
},
...
]
}
Jersey has wrapped my collection of Role representations in some object. Note: The object seems to be connected to the name of the subresource resource method within RolesResource (for example, if the method is named getRoles()
I get {"roles": [...]}
, if the method is named getFoo()
I get {"foo": [...]}
).
My Question: There must be a reason Jersey does this. Why would I want my representation to be wrapped like this? And if there isn't a good reason, how can I get rid of this?
Edit: I'm using Dropwizard version 0.8.2, which it looks like from maven is pulling in Jersey 2.19. It uses Jackson as the JSON provider - again, from maven it looks like the version is 2.5.1. No web.xml as this is a dropwizard application.