泽西 - 杰克逊 - JSON - 日期格式

时间:2013-10-23 09:06:19

标签: json jaxb jersey jackson

我正在尝试配置从Jersey WS的序列化JSON对象返回的日期格式,如下所示:

@Component
@Provider
@Produces("application/json")
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {

    private ObjectMapper mapper = new ObjectMapper();

    public JacksonContextResolver() {
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    }

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

但问题是不会调用 getContext(Class arg0)方法。只调用构造函数 JacksonContextResolver()

然而,以下JAXB的ContextResolver工作正常:

@Component
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

    private JAXBContext context;
    private Class<?>[] types = { 
            UserDto.class, AttachmentDto.class
    };

    public JAXBContextResolver() throws Exception {
        this.context = new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false).build(), types);
    }

    public JAXBContext getContext(Class<?> objectType) {
        return context;
    }
}

有人可以建议是否有任何丢失的配置?

1 个答案:

答案 0 :(得分:0)

我无法让ContextResolver为我工作,但我发现这个博客确实有效:

http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html

相关问题