【问题标题】:Jersey - Jackson - JSON - Date format泽西 - 杰克逊 - JSON - 日期格式
【发布时间】:2013-10-23 09:06:18
【问题描述】:

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

谁能告诉我是否缺少配置?

【问题讨论】:

    标签: json jaxb jersey jackson


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2012-06-07
      • 2013-03-02
      相关资源
      最近更新 更多