Reputation: 1233
I am implementing simple RESTful webservice using Jersey API. My server project is hosted on Apache Tomcat ver 6.0 and it contains asm-3.0.jar, jersey-bundle-1.9.1.jar and jsr311-api-1.1.1.jar.
I have two resource classes. One is UserItemsResource
which is intended to represent collection of UserItem
objects. The other one is UserItemResource
which represents a single UserItem
resource.
Below is code for UserItemsResource
class:
@Path("/useritems")
public class UserItemsResource {
@Context
UriInfo uriInfo;
@Context
Request request;
@Path("{userId}")
public UserItemResource getUserItemResource(@PathParam("userId") long userId) {
return new UserItemResource(uriInfo, request, userId);
}
}
The UserItemResource
class:
public class UserItemResource {
@Context
UriInfo uriInfo;
@Context
Request request;
private long userId;
public UserItemResource(UriInfo uriInfo, Request request, long userId) {
this.uriInfo = uriInfo;
this.request = request;
this.userId = userId;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public UserItem getUserItem() {
return new UserItem(userId, 'M', "Pawan");
}
}
And the UserItem
class:
@XmlRootElement
public class UserItem {
private long userId;
private char sex;
private String displayName;
public UserItem() {
}
public UserItem(long userId, char sex, String displayName) {
this.userId = userId;
this.sex = sex;
this.displayName = displayName;
}
public long getUserId() {
return userId;
}
public char getSex() {
return sex;
}
public String getDisplayName() {
return displayName;
}
public void setUserId(long userId) {
this.userId = userId;
}
public void setSex(char sex) {
this.sex = sex;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
When I invoke the resource (like /useritems/101
), I am getting following response from server.
HTTP/1.1 200 OK
Date: Wed, 24 Oct 2012 11:30:35 GMT
Transfer-Encoding: chunked
Content-Type: application/json
Server: Apache-Coyote/1.1
{
"displayName": "Pawan",
"sex": "77",
"userId": "101"
}
You can see that the value for "sex" attribute is generated as "77", which is ASCII equivalent of character 'M'. I believe this should come as "M" only, so that my client code can successfully parse it back to 'M'. I am using Jackson API (ver 2.0.2) to parse the json entity in the server response back to object of UserItem class.
Am I missing something? Or is this a bug?
Upvotes: 1
Views: 597
Reputation: 10379
Jersey supports few JSON notations and each one of them has a slightly different convention on how the resulting JSON should look like. You can see the difference between notations in this JavaDoc. The default one is MAPPED
which put quotes around numbers in JSON output as you've already found out.
There are two things you can do:
NATURAL
JSON notation which handles numbers as you'd expect. To do this you need to provide a custom ContextResolver and register it in your application. Examples how to achieve this can be found in Jersey User Guide (JSON Support - Configuration Options) or in one of the samples json-from-jaxb (see JAXBContextResolver).Upvotes: 1