我正在尝试配置从 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;
}
}
任何人都可以建议是否缺少任何配置?