ModelMapper возвращает Null в полях DTO при сопоставлении с Entity в DTO

ModelMapper возвращает null в моих полях DTO при сопоставлении объекта Entity с Dto. Кто-нибудь, объясните, почему я получаю ответ Null.

TestEntity

@Data
@Entity
@Table(name="TestEntity")
public class TestEntity {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name="test_name")
    private String test_name;
}

TestEntityDto

@Data
public class TestEntityDto {

    private long id;
    private String test_name;
}

TestService

@Service
public class TestService {

    @Autowired
    TestEntityRepo testEntityRepo;

    public TestEntityDto getAllTestList() {
        List<TestEntity> testEntity= testEntityRepo.findAll();
        ModelMapper mapper = new ModelMapper();
        TestEntityDto testEntityDto = mapper.map(TestEntity.class, TestEntityDto.class);

        return  testEntityDto;
    }
}

Фактический результат:

{
   "id": 0,
   "test_name": null
}

Ожидаемый результат:

{
   "id": 1,
   "test_name": "Test"
}

person Muhammad Faizan    schedule 18.04.2020    source источник


Ответы (2)


Вы пытаетесь сопоставить List<TestEntity> с TestEntityDto, что неверно. Попробуйте отобразить для каждого TestEntity с помощью ModelMapper и составить список TestEntityDto.

public List<TestEntityDto> getAllTestList() {

    List<TestEntity> testEntity= testEntityRepo.findAll();
    List<TestEntityDto> testEntityDtos = new ArrayList<TestEntityDto>();
    ModelMapper mapper = new ModelMapper();
    mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    for(TestEntity perTestEntity :testEntity){
        TestEntityDto testEntityDto = mapper.map(perTestEntity , TestEntityDto.class);
         testEntityDtos.add(testEntityDto);
    }
    return  testEntityDtos;

}
person Eklavya    schedule 18.04.2020

Вы используете private String test_name; в классе, но в dto нет поля с именем test_name.

Используйте private String testName; в TestEntity классе.

Or

Используйте private String test_name; в TestEntityDto классе.

Обновите, используйте сопоставление STRICT и используйте testEntity в качестве источника.

ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
TestEntityDto testEntityDto = mapper.map(testEntity, TestEntityDto.class);
person Kmarlon Pereira    schedule 18.04.2020
comment
Я обновляю свой вопрос, я переименовал поле в своем dto, все та же проблема - person Muhammad Faizan; 18.04.2020
comment
проблема сохраняется. даже я пробовал с этими свойствами ModelMapper modelMapper=new ModelMapper(); modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); modelMapper.getConfiguration().setDestinationNameTokenizer(NameTokenizers.UNDERSCORE); modelMapper.getConfiguration().setSourceNameTokenizer(NameTokenizers.UNDERSCORE); modelMapper.getConfiguration().setFieldAccessLevel(Configuration.AccessLevel.PRIVATE); TestEntityDto testEntityDto = modelMapper.map(testEntity, TestEntityDto.class); - person Muhammad Faizan; 18.04.2020