模型映射器空值跳过

Ach*_*Sai 5 java null modelmapper

Class A {
    private String a;
    private String b;
    private B innerObject;
}
Class B {
    private String c;
}
Run Code Online (Sandbox Code Playgroud)

就我而言,字符串 b 可能带有空值。我的模型映射器配置如下:

ModelMapper mapper = new ModelMapper();
    mapper.getConfiguration()
    .setFieldMatchingEnabled(true)
    .setMatchingStrategy(MatchingStrategies.LOOSE)
    .setFieldAccessLevel(AccessLevel.PRIVATE)
    .setSkipNullEnabled(true)
    .setSourceNamingConvention(NamingConventions.JAVABEANS_MUTATOR);
Run Code Online (Sandbox Code Playgroud)

当我映射对象时,我得到 b=null 值的目标对象。

试图远离此处显示的策略:SO-问题

我错过了什么?

小智 9

你有没有试过这个配置:

modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
Run Code Online (Sandbox Code Playgroud)


fel*_*gnu 7

我宁愿这样:

@Configuration
public class ModelMapperConfig {

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setSkipNullEnabled(true);

        return modelMapper;
    }
}
Run Code Online (Sandbox Code Playgroud)