杰克逊注释

编程入门 行业动态 更新时间:2024-10-28 00:21:48
本文介绍了杰克逊注释 - 如何重命名元素名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

什么是杰克逊JSON注解的方式当量为以下JAX-B注解?

我需要产生JSON,而不是XML和需要知道在JAX-B等价表示常规杰克逊注释。

  • 重命名域。
  • 使用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序列化和反序列化。

    有关更详细的信息: wiki.fasterxml/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: wiki.fasterxml/JacksonFAQ

    更多推荐

    杰克逊注释

    本文发布于:2023-08-01 12:23:46,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1268285.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:杰克逊   注释

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!