0

我正在尝试配置从 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;
    }
}

任何人都可以建议是否缺少任何配置?

4

1 回答 1

0

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

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

于 2013-12-17T09:29:03.620 回答