杰克逊注释 - 如何重命名元素名称? [英] Jackson annotation - How to rename element names?
问题描述
什么是杰克逊JSON注解的方式当量为以下JAX-B注解?
我需要产生JSON,而不是XML和需要知道在JAX-B等价表示常规杰克逊注释。
- 重命名域。 li>
- 使用getter方法,而不是领域。
这些功能如果JSON / XML元素名称是一个java的保留字特别重要
像新
,公共
,静态
等。
所以,我们必须命名POJO领域_new_,_public_,_static_等,分别为
但使用JAX-B注释它们重命名回新,公,静等
在生成的XML(和JSON)的元素。
重命名字段
@XmlAccessorType(XmlAccessType.FIELD)
公共类Person {
@XmlElement(必填= TRUE)
受保护的字符串名称;
@XmlElement(必填= TRUE)
受保护的字符串地址;
@XmlElement(NAME =包工头)
保护布尔_restricted_;
@XmlElement(NAME =新)
保护布尔_new_;
}
重定向到使用属性的getter (我觉得这是它是如何在JAX-B完成)
@XmlAccessorType(XmlAccessType.PROPERTY)
公共类Person {
受保护的字符串名称;
受保护的字符串地址;
保护布尔_restricted_;
保护布尔_new_; @XmlElement(必填= TRUE)
保护字符串的getName(){返回名称;}
@XmlElement(必填= TRUE)
保护字符串的getAddress(){返回地址;}
@XmlElement(NAME =包工头)
保护布尔getRestricted(){返回_restricted_;}
@XmlElement(NAME =新)
保护布尔GETNEW(){返回_new_;}
}
也许这是一个有点晚了,但无论如何..
您可以重命名一个属性只是添加
@JsonProperty(承包人)
和默认杰克逊使用getter和setter序列化和反序列化。
有关更详细的信息: http://wiki.fasterxml.com/JacksonFAQ
What is the equiv way in Jackson json annotation for the following jax-b annotations?
I need to produce json rather than xml and need to know the conventional jackson annotations that is equivalently denoted in jax-b.
- rename a field.
- use getters instead of fields.
These features are especially crucial if the json/xml element name is a java reserved word
like "new
", "public
", "static
", etc.
So that we have to name the POJO fields as "_new_", "_public_", "_static_", etc, respectively,
but use jax-b annotation to rename them back to "new", "public", "static", etc in the generated XML (and json) elements.
Renaming a field
@XmlAccessorType(XmlAccessType.FIELD)
public class Person{
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String address;
@XmlElement(name = "contractor")
protected boolean _restricted_ ;
@XmlElement(name = "new")
protected boolean _new_ ;
}
Redirect to using property getter (I think this is how it is done in jax-b)
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Person{
protected String name;
protected String address;
protected boolean _restricted_ ;
protected boolean _new_ ;
@XmlElement(required = true)
protected String getName() {return name;}
@XmlElement(required = true)
protected String getAddress() {return address;}
@XmlElement(name = "contractor")
protected boolean getRestricted() {return _restricted_;}
@XmlElement(name = "new")
protected boolean getNew(){return _new_;}
}
Probably it's a bit late but anyway..
you can rename a property just adding
@JsonProperty("contractor")
And by default Jackson use the getter and setter to serialize and deserialize.
For more detailed information: http://wiki.fasterxml.com/JacksonFAQ
这篇关于杰克逊注释 - 如何重命名元素名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!