jackson - ObjectMapper#setDefaultSetterInfo( JsonSetter.Value.forValueNulls( Nulls.AS_EMPTY) does not work for custom classes in

I would initialize null or absent lists with empty collections when deserializing. For simple data type

I would initialize null or absent lists with empty collections when deserializing. For simple data types like string, long etc. this works with this setting.

However, as soon as I define my own class this no longer works.

class FooTest {
    
       @Autowired
       private ObjectMapper objectMapper;
    
       @Test
       void empty() throws JsonProcessingException {
    
          objectMapper.setDefaultSetterInfo( JsonSetter.Value.forValueNulls( Nulls.AS_EMPTY ) );
          final Foo foo = objectMapper.readValue( "{}", Foo.class );
          final Foo foo1 = objectMapper.readValue( "{\"items\": null,\"items1\": null}", Foo.class );
    
       }
    
       @JsonIgnoreProperties( ignoreUnknown = true )
       public class Foo {
    
          @JsonDeserialize( as = LinkedHashSet.class )
          private Set<String> items;
    
          @JsonDeserialize( as = LinkedHashSet.class )
          private Set<Bar> items1;
    
          @JsonCreator
          public Foo(
                @JsonProperty( "items" ) final Set<String> items,
                @JsonProperty( "items1" ) final Set<Bar> items1 ) {
             this.items = items;
             this.items1 = items1;
    
          }
       }
    
       @JsonIgnoreProperties( ignoreUnknown = true )
       public class Bar {
    
          @JsonCreator
          public Bar( ){
    
          }
       }
    }

Does anyone know why?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744838654a4596417.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信