云端点不尊重杰克逊注释 [英] cloud endpoint not respecting Jackson annotations

查看:136
本文介绍了云端点不尊重杰克逊注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很少有 date 字段的类组织,如下所示。

I have a class Organization with few date fields as follows.

public class Organization {

    private String _id;

    private String name;

    @JsonDeserialize(using=JsonDateDeserializer.class)
    @JsonSerialize(using=JsonDateSerializer.class)
    private Date createdTime;

// getters and setters

}

为了在客户端以简单的方式处理日期,我将 date 转换为 long 并使用这些JsonSerializer发送给客户端如下所示:

To handle the date in simple way on client side I convert date to long and send it to client using these JsonSerializer as follows

public class JsonDateSerializer extends JsonSerializer<Date>{

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
        gen.writeNumber(date.getTime());
    }

}

我有一个将执行的端点

I have a endpoint which will execute get organization and insert the results in response map like this.

@ApiMethod(name="organization.get", path="organizations/{organizationId}", httpMethod=HttpMethod.GET)
public Map<String, Object> organizationDetails(@Named("organizationId") String organizationId){
    Organization org = DB.getOrganization("123");
    Map<String, Object> response = new HashMap<String, Object>();
    response.put("status", HttpServletResponse.SC_OK);
    response.put("success", true);
    response.put("entity", org);
    return response;
} 

但是我在客户端看到的最终生成的JSON是

But the end resultant JSON I see on client side is

{
  "status" : 200,
  "entity" : [ {
    "_id" : "966a03b3-8e46-41ee-b330-6533409b2b4a",
    "name" : "POKURI",    
    "createdTime" : "2015-05-16T15:02:31.499+05:30"
  } ],
  "success" : true
}

一些格式而不是很长。如果我在不使用云端点的情况下使用Jackson ObjectMapper 进行转换,我会以预期的方式得到响应。为什么云端点不尊重杰克逊注释?有没有办法配置它?

Here the date coming in some format instead of long. If I convert the same using Jackson ObjectMapper without using cloud endpoints I am getting response in expected way. Why cloud endpoints not respecting Jackson annotations? Is there a way to configure that?

注意:即使我观察到 long 如果您使用云端点,则在客户端以字符串形式出现。我正在使用 appengine SDK 1.9.19

Note: Even I observed that long is coming as string on client side if you use cloud endpoints. I am using appengine SDK 1.9.19

推荐答案

您不应该使用Jackson注释端点(如果你这样做,你必须使用重新包装的版本,而不是你自己的)。你可以用 @ApiResourceProperty @ApiTransformer 来做你想做的事情,这取决于你想做什么。请参阅此处的注释文档。

You aren't supposed to use Jackson annotations with Endpoints (and if you do, you have to use the repackaged version, not your own). You can do what you want with @ApiResourceProperty or @ApiTransformer, depending on how you want to do it. See the annotation documentation here.

关于 long ,由于浮点不精确性,它被串行化为一个字符串 - 不可能使用double来精确地表示所有长整型值,这是JavaScript和 JSON.parse 将存储数字的地方,所以它总是以字符串形式传输。我们的任何Endpoint客户端库都会自动将它们转换为正确的数据类型。

Regarding long, it is serialized as a string due to floating point imprecision--it's not possible to represent all values of a long accurately using a double, which is what JavaScript and JSON.parse will store a numeric in, so it is always transmitted as a string. Any of our client libraries for Endpoints automatically convert them to the correct data type.

这篇关于云端点不尊重杰克逊注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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