Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

java - Unable to map multiple values using mapstruct and lombok @Value and @Builder, works with @Data

I have a mapping that looks like this

@Mapper(componentModel = "spring") 
public interface CreateCdiMapper {
    CreateCdiMapper INSTANCE = Mappers.getMapper(CreateCdiMapper.class);


    @Mapping(source = "data.attributes.deviationShortDescription", target = "fields.summary")
    @Mapping(source = "included.genericDeviationId", target = "fields.deviationId")
    DeviationDto jsonToCreateJiraCdi(Deviation createDeviation);
}

The target is made immutable using lomboks @Value, and also has @Builder. My mapping works fine if I comment out either of fields, i.e.

//@Mapping(source = "data.attributes.deviationShortDescription", target = "fields.summary")
@Mapping(source = "included.genericBrandInternalReleation", target = "fields.deviationId")
DeviationDto jsonToCreateJiraCdi(Deviation createDeviation);

and

@Mapping(source = "data.attributes.deviationShortDescription", target = "fields.summary")
//@Mapping(source = "included.genericBrandInternalReleation", target = "fields.deviationId")
DeviationDto jsonToCreateJiraCdi(Deviation createDeviation);

both work fine. I can see that lomboks Builder is being used in the generated classes. But if I leave both mappings uncommented as in the first example above, I get these errors saying the both fields are missing write accessors.

"summary" has no write accessor in Fields for target name "fields.summary".
"deviationId" has no write accessor in Fields for target name "fields.deviationId"

I have in my build.gradle

ext {
    springBootVersion = '2.4.2'
    mapstructVersion = "1.4.1.Final"
    lombokVersion = "1.18.16"
    lombokMapstructBindingVersion = "0.2.0"
}
implementation "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}", "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"

Can someone please help me understand why this happens? If I remove @Value and @Builder and instead go with @Data, everything works fine.

Adding relevant classes

@Value
@Builder
public class Deviation{
    Fields fields;
}

@Value
@Builder
public class Fields {
    String summary;
    String deviationId;
}
question from:https://stackoverflow.com/questions/65943918/unable-to-map-multiple-values-using-mapstruct-and-lombok-value-and-builder-wo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...