簡體   English   中英

將 Mapstruct 升級到 1.4.2.Final

[英]Upgrade Mapstruct to 1.4.2.Final

我正在將項目mapstruct依賴項從1.2.0.Final升級到1.4.2.Final 一切都很順利,因為落在了一個復雜的映射器上,它看起來像下面的代碼片段:

@Mapper(componentModel = "spring")
public abstract class CustomMapper{

@Named("mapTestla")
@Mapping(target = "f", source = "t.f") 
public abstract TestlaDTO mapTestla(Testla t) {}

@Named("mapBar")
@Mapping(target = "t", source = "b.t", qualifiedByName = "mapTestla") 
public abstract BarDTO mapBar(Bar b) {}

@Mappings({@Mapping(target = "a", source = "f.a"), 
@Mapping(target = "b", source = "f.b", qualifiedByName = "mapBar")})
public abstract FooDTO mapFoo(Foo f) {}

}


class Foo {
 String a;
 Bar b;
}

class Bar {
 Tesla t;
}

class Testla {
String f;
}


class FooDTO {
 String a;
 Bar b;
}

class BarDTO {
 TeslaDTO t;
}

class TestlaDTO {
String f;
}

正如您在錯誤中看到的那樣,盡管 mapstruct 使用@Named進行了注釋,但並未檢測到引用的方法。 它看起來像一個spring aop代理問題..

對這個問題有任何想法嗎?

PS:我不想將映射器拆分為多個映射器,因為我們的進度落后於計划,這可能會導致影響。

錯誤得到:

錯誤:限定符錯誤。 找不到使用@Named#value: [mapBar] 注釋的方法。 有關更多信息,請參閱https://mapstruct.org/faq/#qualifier

錯誤:限定符錯誤。 找不到使用@Named#value: [mapTestla] 注釋的方法。 有關更多信息,請參閱https://mapstruct.org/faq/#qualifier

您似乎從不同於 MapStruct 庫中導入了不正確的@Named注釋。
您的示例適用於 MapStruct 1.4.2.Final。

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;


@Mapper(componentModel = "spring")
public abstract class CustomMapper{

    @Named("mapTestla")
    @Mapping(target = "f", source = "t.f")
    public abstract TestlaDTO mapTestla(Testla t);

    @Named("mapBar")
    @Mapping(target = "t", source = "b.t", qualifiedByName = "mapTestla")
    public abstract BarDTO mapBar(Bar b);

    @Mappings({@Mapping(target = "a", source = "f.a"),
           @Mapping(target = "b", source = "f.b", qualifiedByName = "mapBar")})
    public abstract FooDTO mapFoo(Foo f) ;
}

生成的代碼:

@Component
public class CustomMapperImpl extends CustomMapper {
    @Override
    public TestlaDTO mapTestla(Testla t) {
        if ( t == null ) {
            return null;
        }
        TestlaDTO testlaDTO = new TestlaDTO();
        testlaDTO.f = t.f;
        return testlaDTO;
    }

    @Override
    public BarDTO mapBar(Bar b) {
        if ( b == null ) {
            return null;
        }
        BarDTO barDTO = new BarDTO();
        barDTO.t = mapTestla( b.t );
        return barDTO;
    }

    @Override
    public FooDTO mapFoo(Foo f) {
        if ( f == null ) {
            return null;
        }
        FooDTO fooDTO = new FooDTO();
        fooDTO.a = f.a;
        fooDTO.b = mapBar( f.b );
        return fooDTO;
    }
}

根據文檔: 映射對象引用
您不需要通過方法限定符顯式指定內部對象映射。 為所有對象指定映射方法就足夠了。 MapStract 將自動限定它們。

@Mapper(componentModel = "spring")
public abstract class CustomMapper {
    public abstract TestlaDTO mapTestla(Testla t);

    public abstract BarDTO mapBar(Bar b);

    public abstract FooDTO mapFoo(Foo f) ;
}

生成的代碼:

@Component
public class CustomMapperImpl extends CustomMapper {

    @Override
    public TestlaDTO mapTestla(Testla t) {
        if ( t == null ) {
            return null;
        }
        TestlaDTO testlaDTO = new TestlaDTO();
        testlaDTO.f = t.f;
        return testlaDTO;
    }

    @Override
    public BarDTO mapBar(Bar b) {
        if ( b == null ) {
            return null;
        }
        BarDTO barDTO = new BarDTO();
        barDTO.t = mapTestla( b.t );
        return barDTO;
    }

    @Override
    public FooDTO mapFoo(Foo f) {
        if ( f == null ) {
            return null;
        }
        FooDTO fooDTO = new FooDTO();
        fooDTO.a = f.a;
        fooDTO.b = mapBar( f.b );
        return fooDTO;
    }
}

如您所見,MapStruct 生成了相同的映射器代碼,沒有額外的注釋!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM