我目前有这个:
@JacksonXmlRootElement(namespace = "some-ns", localName = "queryResult")
public class QueryResult {
...
}
现在我需要通过配置文件(即在运行时设置)使"some-ns"
可配置。
实现这一目标的最佳方式是什么?
答案 0 :(得分:2)
您可以撰写自定义AnnotationIntrospector
:
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector;
public class MyJacksonXmlAnnotationIntrospector extends JacksonXmlAnnotationIntrospector
{
private static final long serialVersionUID = 1L;
@Override
public PropertyName findRootName(AnnotatedClass ac) {
PropertyName pn = super.findRootName(ac);
return pn.withNamespace("custom");
}
}
然后将其传递给jackson mapper:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setAnnotationIntrospector(new MyJacksonXmlAnnotationIntrospector());
xmlMapper.writeValue(System.out, new QueryResult());