zoukankan      html  css  js  c++  java
  • springboot 存在superBuilder时使用Mapstruct

    问题

    springboot在使用mapstruct对对象进行转换时遇到一个问题,即在对象使用了@SuperBuilder注解时mapstruct生成的转换类会报错(Mapper放在涉及转换的类内,另外写class不会报错),报错如下

    
    @Data
    @SuperBuilder(toBuilder = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public class TestSuperClass {
    
        private String column1;
        private String column2;
    
    }
    
    @EqualsAndHashCode(callSuper = true)
    @Data
    @SuperBuilder(toBuilder = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public class TestClass extends TestSuperClass{
    
        private String column3;
        private String column4;
    
        @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
        public interface TestClassModelMapper {        
            TestTargetClass doToTarget(TestClass testClass);
        }
    
        public static final TestClassModelMapper INSTANCE = Mappers.getMapper(TestClassModelMapper.class);
    }
    
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public class TestTargetClass {
    
        private String column1;
        private String column2;
        private String column3;
        private String column4;
    
    }
    
    

    以上为各实体类

    @Generated(
        value = "org.mapstruct.ap.MappingProcessor",
        date = "2021-07-06T15:28:32+0800",
        comments = "version: 1.4.2.Final, compiler: javac, environment: Java 1.8.0_251 (Oracle Corporation)"
    )
    public class TestClass$TestClassModelMapperImpl implements TestClassModelMapper {
    
        @Override
        public TestTargetClass doToTarget(TestClass testClass) {
            if ( testClass == null ) {
                return null;
            }
    
            TestTargetClassBuilder testTargetClass = TestTargetClass.builder();
    
            testTargetClass.column1( testClass.getColumn1() );
            testTargetClass.column2( testClass.getColumn2() );
            testTargetClass.column3( testClass.getColumn3() );
            testTargetClass.column4( testClass.getColumn4() );
    
            return testTargetClass.build();
        }
    
        @Override
        public TestClass doToDo(TestClass testClass) {
            if ( testClass == null ) {
                return null;
            }
    
            TestSuperClassBuilder<?, ?> testClass1 = TestSuperClass.builder();
    
            testClass1.column1( testClass.getColumn1() );
            testClass1.column2( testClass.getColumn2() );
    
            return testClass1.build();
        }
    }
    

    以上为mapstruct生成的转换class,很明显生成的转换类内调用了父类的Builder导致返回类型错误

    解决方案

    TestClass内部Mapper的注解@Mapper内添加参数builder = @Builder(disableBuilder = true),即

    @EqualsAndHashCode(callSuper = true)
    @Data
    @SuperBuilder(toBuilder = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public class TestClass extends TestSuperClass implements Serializable {
        private static final long serialVersionUID = 1L;
    
        private String column3;
        private String column4;
    
        @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, builder = @Builder(disableBuilder = true))
        public interface TestClassModelMapper {
            TestTargetClass doToTarget(TestClass testClass);
    
            TestClass doToDo(TestClass testClass);
        }
    
        public static TestClass.TestClassModelMapper INSTANCE = Mappers.getMapper(TestClass.TestClassModelMapper.class);
    }
    
  • 相关阅读:
    [Android]Android系统启动流程源码分析
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q63-Q65)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q60-Q62)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q57-Q59)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q54-Q56)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q51-Q53)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q48-Q50)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q45-Q47)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q40-Q44)
    Sharepoint学习笔记—习题系列--70-573习题解析 -(Q35-Q39)
  • 原文地址:https://www.cnblogs.com/wandoupeas/p/14977230.html
Copyright © 2011-2022 走看看