java字段映射配置,使用Jackson进行映射时,将默认值设置为空字段

I am trying to map some JSON objects to Java objects with Jackson. Some of the fields in the JSON object are mandatory(which I can mark with @NotNull) and some are optional.

After the mapping with Jackson, all the fields that are not set in the JSON object will have a null value in Java. Is there a similar annotation to @NotNull that can tell Jackson to set a default value to a Java class member, in case it is null?

Edit:

To make the question more clear here is some code example.

The Java object:

class JavaObject {

@NotNull

public String notNullMember;

@DefaultValue("Value")

public String optionalMember;

}

The JSON object can be either:

{

"notNullMember" : "notNull"

}

or:

{

"notNullMember" : "notNull",

"optionalMember" : "optional"

}

The @DefaultValue annotations is just to show what I am asking. It's not a real annotation. If the JSON object is like in the first example I want the value of the optionalMember to be "Value" and not null. Is there an annotation that does such a thing?

解决方案

There is no annotation to set default value.

You can set default value only on java class level:

public class JavaObject

{

public String notNullMember;

public String optionalMember = "Value";

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值