ModelMapper return 从实体映射到 DTO 时 DTO 字段中为空
ModelMapper return Null in DTO fields while mapping from Entity to DTO
ModelMapper returns null
在我的 DTO 字段中,同时从实体对象映射到 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"
}
您在 class 中使用 private String test_name;
但在 dto 中没有名为 test_name
的字段。
在TestEntity
class中使用private String testName;
。
或
在TestEntityDto
class中使用private String test_name;
。
更新 使用 STRICT
映射并使用 testEntity 作为源。
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
TestEntityDto testEntityDto = mapper.map(testEntity, TestEntityDto.class);
您试图将 List<TestEntity>
映射到 TestEntityDto
,这是错误的。
尝试使用 ModelMapper 为每个 TestEntity
映射并制作 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;
}
ModelMapper returns null
在我的 DTO 字段中,同时从实体对象映射到 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"
}
您在 class 中使用 private String test_name;
但在 dto 中没有名为 test_name
的字段。
在TestEntity
class中使用private String testName;
。
或
在TestEntityDto
class中使用private String test_name;
。
更新 使用 STRICT
映射并使用 testEntity 作为源。
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
TestEntityDto testEntityDto = mapper.map(testEntity, TestEntityDto.class);
您试图将 List<TestEntity>
映射到 TestEntityDto
,这是错误的。
尝试使用 ModelMapper 为每个 TestEntity
映射并制作 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;
}