[英]Jersey - Jackson - JSON - Date format
我正在嘗試配置從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;
}
}
任何人都可以建議是否缺少任何配置?
我也無法使ContextResolver為我工作,但是我發現這個博客確實有效:
http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.