java jackson 生成_java-Jackson JSON反序列化-合成列表获取器

停用DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS.

mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false);

使用静态导入使该行更短.

或者,如果您希望批注仅为此一个属性配置内容,而不指定上述全局设置,则将某些内容标记为“团队”的设置者.

public class Foo

{

@JsonSetter("teams")

public void asdf(List teams)

{

System.out.println("hurray!");

}

public List getTeams()

{

// generate unmodifiable list, to fail if change attempted

return Arrays.asList(new Team());

}

public static void main(String[] args) throws Exception

{

ObjectMapper mapper = new ObjectMapper();

String fooJson = mapper.writeValueAsString(new Foo());

System.out.println(fooJson);

// output: {"teams":[{"name":"A"}]}

// throws exception, without @JsonSetter("teams") annotation

Foo fooCopy = mapper.readValue(fooJson, Foo.class);

// output: hurray!

}

}

class Team

{

public String name = "A";

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值