Jersey将字符值序列化为ASCII等效数字字符串 [英] Jersey serializes character value to ASCII equivalent numeric string

查看:129
本文介绍了Jersey将字符值序列化为ASCII等效数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey API实现简单的RESTful Web服务。我的服务器项目托管在Apache Tomcat ver 6.0上,它包含 asm-3.0.jar jersey-bundle-1.9.1.jar jsr311-api- 1.1.1.jar 即可。

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.

我有两个资源类。一个是 UserItemsResource ,它用于表示 UserItem 对象的集合。另一个是 UserItemResource ,它代表一个 UserItem 资源。

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.

以下是 UserItemsResource 类的代码:

@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);
    }
}

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");
    }
}

以及 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;
    }
}

当我调用资源时(如 / useritems / 101 ),我收到服务器的响应。

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"
}

您可以看到sex属性的值生成为77,这是字符M的ASCII等价物。我相信这应该只是M,所以我的客户端代码可以成功地将其解析回M。我使用 Jackson API(版本2.0.2)将服务器响应中的json实体解析回UserItem类的对象。

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我错过了什么?或者这是一个错误?

Am I missing something? Or is this a bug?

推荐答案

Jersey支持几种JSON符号,并且每个符号都有一个略有不同的约定JSON应该是这样的。您可以在 JavaDoc的。默认值为 MAPPED ,它会在您已经找到的JSON输出中放置数字。

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.

那里你可以做两件事:

  • 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).

这篇关于Jersey将字符值序列化为ASCII等效数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆