Jersey serializes character value to ASCII equivalent numeric string

只谈情不闲聊 提交于 2019-12-08 04:58:45

问题


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?


回答1:


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:

  • turn on the Jackson POJO support which uses the Jackson library to create JSON output (maybe this would be a better option if you're using Jackson on the client side as well). There is also an example how to use it - JacksonJsonProvider.
  • use 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).


来源:https://stackoverflow.com/questions/13048658/jersey-serializes-character-value-to-ascii-equivalent-numeric-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!