注意:这与 StackOverflow 上的其他问题不同,因为它们通过手动映射两个类来解决此问题。由于 ScheduleSource 和 ScheduleTarget 是完全相同的类,我希望它们自动映射。
你好,
我有 2 个类 ScheduleSource 和 ScheduleTarget。它们具有完全相同的属性。
当我尝试使用 MapStruct 从 ScheduleSource 映射到 ScheduleTarget 时,出现错误:
Can't map property "java.util.Optional<java.time.LocalDate> startDate" to "java.time.LocalDate startDate". Consider to declare/implement a mapping method: "java.time.LocalDate map(java.util.Optional<java.time.LocalDate> value)
我附上了这两个文件。你能帮忙吗?
文件是:
ScheduleSource、ScheduleTarget - 两个 Java Bean
ScheduleMapper - 映射类。
调度映射器
package testStructMap;
import org.mapstruct.*;
import org.mapstruct.factory.*;
@Mapper
public interface ScheduleMapper {
ScheduleMapper INSTANCE = Mappers.getMapper( ScheduleMapper.class );
ScheduleTarget scheduleSourceToScheduleTarget(ScheduleSource scheduleSource);
}
ScheduleSource.java、ScheduleTarget.java - 相同的结构
package testStructMap;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;
import javax.validation.constraints.*;
public class ScheduleSource {
@FutureOrPresent
@NotNull
private LocalDate startDate;
@NotBlank
private String repeatType;
@Positive
private Integer occurrences;
public Optional<LocalDate> getStartDate() {
return Optional.ofNullable(startDate);
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
public String getRepeatType() {
return repeatType;
}
public void setRepeatType(String repeatType) {
this.repeatType = repeatType;
}
public Optional<Integer> getOccurrences() {
return Optional.ofNullable(occurrences);
}
public void setOccurrences(Integer occurrences) {
this.occurrences = occurrences;
}
}
翻翻过去那场雪
杨魅力
尚方宝剑之说
相关分类