Jersey + Jackson JSON日期格式序列化 - 如何更改格式或使用自定义JacksonJsonProvider [英] Jersey + Jackson JSON date format serialization - how to change the format or use custom JacksonJsonProvider

查看:1505
本文介绍了Jersey + Jackson JSON日期格式序列化 - 如何更改格式或使用自定义JacksonJsonProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey + Jackson为我的应用程序提供REST JSON服务层。我遇到的问题是默认的日期序列化格式如下:

I am using Jersey + Jackson to provide REST JSON services layer for my application. The problem I have is that the default Date serialization format looks like that:

"CreationDate":1292236718456

起初我认为它是一个UNIX时间戳......但它太长了。我的客户端JS库在反序列化这种格式时遇到了问题(它支持一堆不同的日期格式,但我认为不支持这种格式)。我想更改格式,以便我的库(例如ISO)可以使用它。我该怎么做...我找到了一段可以帮助的代码,但是......我把它放在哪里,因为我不控制杰克逊序列化器实例化(泽西岛)?

At first I thought it is a UNIX timestamp... but it is too long for that. My client-side JS library has problems deserializing this format (it supports a bunch of different date formats but not this one I suppose). I want to change the format so that it can be consumable by my library (to ISO for example). How do I do that... I found a piece of code that could help but... where do I put it as I don't control the Jackson serializer instantiation (Jersey does)?

objectMapper.configure(
    SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

我还发现此代码为自定义 JacksonJsonProvider - 问题是..如何让我所有的POJO课程使用它?

I also found this code for custom JacksonJsonProvider - the question is .. how do I make all my POJO classes use it?

@Provider
public class MessageBodyWriterJSON extends JacksonJsonProvider {

    private static final String DF = "yyyy-MM-dd’T'HH:mm:ss.SSSZ";

    @Override
    public boolean isWriteable(Class arg0, Type arg1, Annotation[] arg2,
            MediaType arg3) {
        return super.isWriteable(arg0, arg1, arg2,
                arg3);
    }
    @Override
    public void writeTo(Object target, Class arg1, Type arg2, Annotation[] arg3,
            MediaType arg4, MultivaluedMap arg5, OutputStream outputStream)
            throws IOException, WebApplicationException {
            SimpleDateFormat sdf=new SimpleDateFormat(DF);

        ObjectMapper om = new ObjectMapper();
        om.getDeserializationConfig().setDateFormat(sdf);
        om.getSerializationConfig().setDateFormat(sdf);
        try {
            om.writeValue(outputStream, target);
        } catch (JsonGenerationException e) {
            throw e;
        } catch (JsonMappingException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
    }
}


推荐答案

对于它的价值,该数字是标准Java时间戳(由JDK类使用); Unix存储秒,Java毫秒,这就是为什么它的值更大。

For what it's worth, that number is standard Java timestamp (used by JDK classes); Unix stores seconds, Java milliseconds, which is why it's bit larger value.

我希望有一些关于如何将ObjectMapper注入Jersey的文档(它应该遵循通常的方式来注入提供的对象)。但另外你可以覆盖JacksonJaxRsProvider来指定/配置ObjectMapper并注册它;这就是泽西岛本身所做的,有多种方法可以做到。

I would hope there are some documents as to how to inject ObjectMapper into Jersey (it should follow the usual way to inject provided object). But alternatively you could override JacksonJaxRsProvider to specify/configure ObjectMapper and register that; this is what Jersey itself does, and there are multiple ways to do it.

这篇关于Jersey + Jackson JSON日期格式序列化 - 如何更改格式或使用自定义JacksonJsonProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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