13

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.

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.

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?

4

3 回答 3

18

您需要向ContextResolverResteasy 注册您的实现。您可以通过使用注释对您的类进行@Provider注释并允许 Resteasy 在启动期间自动扫描它、在 web.xml 中注册它或在扩展的类中注册它javax.ws.rs.core.Application(如果这是您引导 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 文件中是否启用了类路径扫描,如下所示:

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

注意:如果您在 JBoss 7 中部署它,请不要设置resteasy.scancontext 参数,因为它默认启用。

通过 web.xml 注册

将以下上下文参数添加到您的web.xml文件中。参数的值应该是您的ContextResolver.

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

通过应用程序注册

如果您使用 Application 类来配置 Resteasy,您可以将您的提供者添加到服务和提供者集以向 Resteasy 注册,如下所示:

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

更多关于独立配置的信息在这里

于 2013-10-07T20:59:52.773 回答
6

与 JSR310(新 api 日期)一起使用 - LocalDate、LocalDateTime、LocalTime

添加依赖:

<dependency> 
     <groupId>com.fasterxml.jackson.datatype</groupId> 
     <artifactId>jackson-datatype-jsr310</artifactId> 
     <version>2.4.0</version> 
</dependency>

并创建一个提供者来注册模块:

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

public JacksonConfig() throws Exception {

    objectMapper = new ObjectMapper()
                .disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS )
                .disable( SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS )
                .setSerializationInclusion( JsonInclude.Include.NON_NULL )
                .registerModule( new JSR310Module() );

}

@Override
public ObjectMapper getContext( Class<?> arg0 ) {
    return objectMapper;
} }
于 2015-12-23T19:37:46.510 回答
1

只需用(注意字符串文字可以从常量外部化/引用)注释您的字段:

@javax.json.bind.annotation.JsonbDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
Date myDate;
于 2019-01-08T05:29:59.713 回答