我们目前对数据对象进行了一些混合,以便将注释排除在数据对象之外。所以,例如
public class SomeDataObj {
private int a;
public int getA() { return this.a; }
public void setA(final int a) { this.a = a; }
}
public interface SomeDataObjMixin {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "A")
int getA();
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "A")
void setA(int a);
}然后,在我们的对象映射类中,我们有
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class OurXmlMapper extends XmlMapper {
public OurXmlMapper(final ConfigurableCaseStrategy caseStrategy) {
setPropertyNamingStrategy(caseStrategy);
setSerializationInclusion(Include.NON_NULL);
//yadda yadda
addMixin(SomeDataObj.class, SomeDataObjMixin.class);
// etc etc
}但是,出于各种原因,我想在数据对象中的私有字段中添加一个新的注释,而不是getter或setter。有什么方法可以通过混合来维持这种分离呢?我尝试创建一个基本类,作为一个混合体(而不是接口),并在其中添加了新的注释的私有字段。这并没有达到我想要的目的。有什么想法吗?
发布于 2018-05-30 07:04:47
使用混凝土类作为搅拌器工作。
// ******************* Item class *******************
public class Item {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// ******************* ItemMixIn class *******************
@JacksonXmlRootElement(localName = "item-class")
public class ItemMixIn {
@JacksonXmlProperty(localName = "firstName")
private String name;
}
// ******************* test method *******************
public void test() throws Exception {
ObjectMapper mapper = new XmlMapper();
mapper.addMixIn(Item.class, ItemMixIn.class);
Item item = new Item();
item.setName("hemant");
String res = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(item);
System.out.println(res);
}输出:
<item-class>
<firstName>hemant</firstName>
</item-class>我有杰克逊版本的2.9.5。
https://stackoverflow.com/questions/50589620
复制相似问题