【发布时间】:2012-01-05 19:29:29
【问题描述】:
当接收到包含 null 或 "" 值的 JSON 字符串时,如何让 JAXB 保留 null。
字符串:{"id":null,"fname":"John","region":""}
返回对象:
Person {
Integer id = 0
String fname = "John"
Region regions = 0 }
我希望它返回 null 而不是 0
这是我目前所拥有的:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class<?>[] types = {Person.class};
public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
}
public JAXBContext getContext(Class<?> objectType) {
for (Class<?> c : types) {
if (c.equals(objectType)) {
return context;
}
}
return null;
}
}
Person.class 用@XmlRootElement 进行了注释我已经尝试查看Jackson 注释但没有成功。
【问题讨论】:
-
这里有同样的问题。有人为这个问题找到了解决方案。泽西岛似乎忽略了 ObjectMapper
-
同样的问题。我也尝试过使用 ObjectMapper,但 Jersey 也未能成功。我不确定使用 ObjectMapper 是不是最简单的解决方案......
-
我的类似问题已经解决。你可以看看对你有没有帮助...stackoverflow.com/questions/10420641/…