Java 使用MapStruct将2个字符串字段映射到OffsetDateTime

Java 使用MapStruct将2个字符串字段映射到OffsetDateTime,java,spring,mapstruct,Java,Spring,Mapstruct,我有以下DTO,其中有2个字段必须转换为OffsetDateTime: @Data public class AppointmentDTO { private String id; @NotNull private String startTime; @NotNull private String endTime; @NotNull private String timeZoneStart; @NotNull pr

我有以下DTO,其中有2个字段必须转换为OffsetDateTime:

@Data
public class AppointmentDTO {

    private String id;

    @NotNull
    private String startTime;

    @NotNull
    private String endTime;

    @NotNull
    private String timeZoneStart;

    @NotNull
    private String timeZoneEnd;

    // other fields
} 

为了转换,我需要DTO的
时区
字段加上
日期时间格式
。因此,我的尝试是:

@Component
@Mapper(componentModel = "spring")
public interface IAppointmentMapper {

    @Mapping(target = "createdTime", ignore = true)
    Appointment convertAppointmentDTOToAppointment(AppointmentDTO dto, @Context OffsetDateTimeMapper offsetDateTimeMapper);
}
但是,当我构建时,我得到以下错误:
error:(22,17)java:无法将属性“java.lang.String startTime”映射到“java.time.OffsetDateTime startTime”。考虑声明/实现映射方法:“java. Time.OffStestDATE-TimeMmap(JavaLang.Stand value)”.< /Cord>如果在接口上放置<代码>默认MAP()/代码>方法,则不会出错,但我没有正确的<代码>上下文上下文/代码>。

/*default OffsetDateTime map(String value) {
     LocalDateTime localDateTime = LocalDateTime.parse(value);
     ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneOffset.UTC);
     return zonedDateTime.toOffsetDateTime();
}*/

你的方法很有趣。为了正常工作,必须显式忽略不希望MapStruct自动映射的属性

在这种情况下,添加:

@Mapping(target = "startTime", ignore = true)
@Mapping(target = "endTime", ignore = true)
但是,在您的情况下,我将尝试使用源参数作为源参数

比如:

@Component
@Mapper(componentModel = "spring")
public interface IAppointmentMapper {

    @Mapping(target = "createdTime", ignore = true)
    @Mapping(target = "startTime", source = "dto", qualifiedByName = "startTime")
    @Mapping(target = "endTime", source = "dto", qualifiedByName = "endTime")
    Appointment convertAppointmentDTOToAppointment(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter);

    @Named("startTime")
    default OffsetDateTime mapStartTime(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter) {

        LocalDateTime localDateTime = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter); 
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(dto. getTimeZoneStart()));
        return zonedDateTime.toOffsetDateTime()
    }

    @Named("endTime")
    default OffsetDateTime mapEndTime(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter) {

        LocalDateTime localDateTime = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter); 
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(dto. getTimeZoneEnd()));
        return zonedDateTime.toOffsetDateTime()
    }
}
注:
@Named
org.mapstruct.Named

@Mapping(target = "startTime", ignore = true)
@Mapping(target = "endTime", ignore = true)
@Component
@Mapper(componentModel = "spring")
public interface IAppointmentMapper {

    @Mapping(target = "createdTime", ignore = true)
    @Mapping(target = "startTime", source = "dto", qualifiedByName = "startTime")
    @Mapping(target = "endTime", source = "dto", qualifiedByName = "endTime")
    Appointment convertAppointmentDTOToAppointment(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter);

    @Named("startTime")
    default OffsetDateTime mapStartTime(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter) {

        LocalDateTime localDateTime = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter); 
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(dto. getTimeZoneStart()));
        return zonedDateTime.toOffsetDateTime()
    }

    @Named("endTime")
    default OffsetDateTime mapEndTime(AppointmentDTO dto, @Context DateTimeFormatter dateTimeFormatter) {

        LocalDateTime localDateTime = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter); 
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of(dto. getTimeZoneEnd()));
        return zonedDateTime.toOffsetDateTime()
    }
}