java字段映射成其他字段名称,如何将JSON字段名称映射到不同的对象字段名称?...

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值