简体   繁体   中英

MapStruct with AWS SDk V2 Builder

For internal mapping, I created the POJO / DTO for the AWS SDK Models V2

To map the variables I am using the MapStruct using the Mapper annotation but the Implementation generated during the annotation processing is not using any getters, but the setter with the builder is added properly but with null

@Mapper
public interface Ec2Mapper {
LaunchTemplateBlockDeviceMappingModel mapLaunchTemplateBlockDeviceMappingModel(
      LaunchTemplateBlockDeviceMapping launchTemplateBlockDeviceMapping);
}


@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-12-08T15:58:00+0530",
    comments = "version: 1.4.2.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-6.4.1.jar, environment: Java 1.8.0_292 (AdoptOpenJDK)"
)
public class Ec2MapperImpl implements Ec2Mapper {

    @Override
    public LaunchTemplateBlockDeviceMappingModel mapLaunchTemplateBlockDeviceMappingModel(LaunchTemplateBlockDeviceMapping launchTemplateBlockDeviceMapping) {
        if ( launchTemplateBlockDeviceMapping == null ) {
            return null;
        }

        String deviceName = null;
        String virtualName = null;
        LaunchTemplateEbsBlockDeviceModel ebs = null;
        String noDevice = null;

        LaunchTemplateBlockDeviceMappingModel launchTemplateBlockDeviceMappingModel = new LaunchTemplateBlockDeviceMappingModel( deviceName, virtualName, ebs, noDevice );

        return launchTemplateBlockDeviceMappingModel;
    }
}

AWS SDK V2 Models don't have getters with the prefix get, it directly the name of the variable, how to configure the map struct for this scenario.

MapStruct provides a way to define whether a certain method is a getter or a setter through its AccessorNamingStrategy .

In theory you can write a custom AccessorNamingStrategy that would be able to detect the methods from the AWS SDK Java 2 as getters. Keep in mind that plainly returning every method with a return type and no parameters as a getter might lead to false positives.

Ideally the methods should be annotated by AWS SDK and you can then use that to detect if a method is a getter or not. The SDK currently does not do this. You could request this, but I am not sure whether the AWS team will be willing to do such a thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM