本文介绍了使用RESTEasy 3.x更改默认JSON时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RESTEasy使用JSON序列化实现REST服务。目前,日期自1970年以来被序列化为毫秒。为了提高兼容性,我想将我的日期变为两种格式之一;毫秒+时区偏移或ISO 8061。

I am using RESTEasy to implement a REST Service using JSON serialization. Currently, Dates are getting serialized to milliseconds since 1970. To improve compatibility, I would like to get my dates into one of two formats; milliseconds + timezone offset or ISO 8061.

似乎RESTEasy曾经使用Jettison进行JSON序列化,但是从我读过的内容来看,他们已经转向杰克逊了。所有这些都让google搜索得到了很好的帮助。

It seems that RESTEasy used to use Jettison for JSON serialization, but from what I've been reading they've switch to Jackson ... all of this has made googling for help pretty hit or miss.

据我所知,我需要实现一个ContextResolver:

From what I can tell, I need to implement a ContextResolver along the lines of:

    public class JacksonConfig impelments ContextResolver<ObjectMapper>
    {
        private final OBjectMapper objectMapper;

        public JacksonConfig() throws Exception
        {
            objectMapper = new ObjectMapper.configure(
                               SerializationFeature.WRITE_DATE_AS_TIMESTAMPS, false);
        }

        @Override
        public ObjectMapper getContext(Class<?> arg0)
        {
            return objectMapper;
        }
     }

我找不到的东西,我该怎么办?我把它放在哪里?

The thing I haven't been able to find, is what do I do with this? Where do I put it?

所以更大的问题是,我是朝着正确的方向前进并且我的假设是否正确?

So the larger questions are, am I heading in the right direction and are my assumptions correct?

推荐答案

您需要使用Resteasy注册 ContextResolver 实施。您可以通过使用 @Provider 注释来注释您的类,并允许Resteasy在启动期间自动扫描它,在web.xml中注册它,或者在类中注册它。 extends javax.ws.rs.core.Application (如果这就是你引导Resteasy的方式)。

You need to register your ContextResolver implementation with Resteasy. You can do this by annotating your class with the @Provider annotation and allowing Resteasy to automatically scan it during startup, registering it in web.xml, or registering it in a class that extends javax.ws.rs.core.Application (if that is how you are bootstrapping Resteasy).

@Provider
public class JacksonConfig implements ContextResolver<ObjectMapper>
{
    private final ObjectMapper objectMapper;

    public JacksonConfig() throws Exception
    {
        objectMapper = new ObjectMapper.configure(
                           SerializationFeature.WRITE_DATE_AS_TIMESTAMPS, false);
    }

    @Override
    public ObjectMapper getContext(Class<?> arg0)
    {
        return objectMapper;
    }
 }

验证web.xml中是否启用了类路径扫描文件如下:

Verify that classpath scanning is enabled in your web.xml file like so:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

注意:如果要在JBoss 7中部署它,请不要设置 resteasy.scan 上下文参数,因为默认情况下已启用。

NOTE: If you are deploying this in JBoss 7 do not set the resteasy.scan context parameter as it is enabled by default.

将以下上下文参数添加到 web.xml 文件中。参数的值应该是 ContextResolver 的完全限定类名。

Add the following context parameter to your web.xml file. The value of the parameter should be the fully qualified class name of your ContextResolver.

<context-param>
      <param-name>resteasy.providers</param-name>
      <param-value>foo.contextresolver.JacksonConfig</paramvalue>
</context-param>



通过申请注册



如果你是使用Application类配置Resteasy,您可以将您的提供商添加到服务和提供商集合中,以便在Resteasy中注册,如下所示:

Registering via Application

If you are using an Application class to configure Resteasy you can add your provider to the set of services and providers to register with Resteasy like so:

public class MyApp extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {
        HashSet<Class<?>> set = new HashSet<Class<?>>(2);
        set.add(JacksonConfig.class);
        set.add(MyService.class);
        return set;
    }
}

有关独立配置的更多信息,请

More on standalone configuration HERE

这篇关于使用RESTEasy 3.x更改默认JSON时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!