标签: mapstruct

Mapstruct构造函数注入(spring)

在mapstruct 中,1.3.0.Final我们通过构造函数进行依赖注入。文档说:

生成的映射器将注入uses 属性中定义的所有类

(...)

对于抽象类或装饰器,应使用 setter 注入。

我有以下示例:

@Mapper
public abstract class VehicleMapper {

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private BikeMapper bikeMapper;

    @Override
    public VehicleDTO toDto(final Vehicle source) {

        if (source instanceof Car) {
            return carMapper.toDto((Car) source);
        } else if (source instanceof Bike) {
            return bikeMapper.toDto((Bike) source);
        } else {
            throw new IllegalArgumentException();
        }
    }
    (...)
Run Code Online (Sandbox Code Playgroud)

所以在我的例子中它应该看起来像这样(componentModel在maven中定义):

@Mapper
public abstract class VehicleMapper {

    private CarMapper carMapper;
    private BikeMapper bikeMapper;

    @Autowired
    public void setCarMapper(final CarMapper carMapper) …
Run Code Online (Sandbox Code Playgroud)

mapstruct

6
推荐指数
1
解决办法
1万
查看次数

MapStruct继承,不止一个配置原型是application

我的所有实体都扩展了一个具有Customer createdByandString createdById字段的基本实体,还有一些具有相同功能的实体。我想Customer在传输数据时排除整个对象。

其他一些实体扩展了另一个实体,该实体本身扩展了基本实体,因此有以下映射器:

@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface AuditingEntityMapper<Dto, Entity> {

    @Mapping(target = "createdBy", source = "createdById")
    @Mapping(target = "lastModifiedBy", source = "lastModifiedById")
    Dto toDto(Entity entity);

    @Mapping(target = "createdBy", ignore = true)
    @Mapping(target = "lastModifiedBy", ignore = true)
    Entity toEntity(Dto dto);
}


@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface ManageableEntityMapper<ManageableDtoType extends ManageableDTO, ManageableType extends Manageable> {

    ManageableDtoType toDto(ManageableType manageable);

    @Mapping(target = "project", ignore = true)
    ManageableType toEntity(ManageableDtoType dto);
} …
Run Code Online (Sandbox Code Playgroud)

java jhipster mapstruct

6
推荐指数
1
解决办法
3万
查看次数

application.properties 中的 Mapstruct 值

是否可以从application.properties文件中的字段设置值?

我正在寻找类似的东西

    @Mapping(target="version", expression="${application.version}")
    StateDto stateToStateDto(State state);
Run Code Online (Sandbox Code Playgroud)

其中application.version=v1来自application.properties文件。

java spring properties mapper mapstruct

6
推荐指数
2
解决办法
4708
查看次数

使用 Mapstruct 映射类的层次结构

我有一个类层次结构:VehicleDTO是一个基本抽象类。 CarDTO, TruckDTO, VanDTO从它延伸出来。

我在映射器的另一侧有相同的层次结构: VehicleBO <- CarBO, TruckBO, VanBO

我希望将所有映射逻辑合并到一个映射器中。时期。

我已经为公共属性定义了映射,但是当它变得有趣时,我在编译过程中得到了这个异常:

The return type ... is an abstract class or interface.
Provide a non abstract / non interface result type or a factory method. 
Run Code Online (Sandbox Code Playgroud)

那么,我如何指定一个工厂方法,它基于特定属性的值或 pojo 的类,为我创建一个目标对象?我希望有一个能真正解决问题的好的代码片段。

谢谢!

mapstruct

5
推荐指数
1
解决办法
7767
查看次数

MapStruct @Mapping(表达式=“java(...)”)

是否可以在表达式中包含 if-else 或三元运算符之类的条件

@Mapping(expression="java(...)")
Run Code Online (Sandbox Code Playgroud)

我有一个返回 ArrayList 的最后一项的方法,但如果列表为空,它会返回 null。我需要一个条件,所以如果我收到该项目,我可以使用它,或者如果它为空,它将映射为空。

public static MyObjectDetail getLastOne(MyObject myObject) {
    List<MyObjectDetail> details = myObject.getMyObjectDetails();
    if(details.isEmpty()) {
        return null;
    } else {
        return myObject.getLastDetail(myObject);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我当前使用的@Mapping,如果列表不为空,它就可以正常工作。

@Mapping(expression = "java(MyObjectDetailMapper.getLastOne(myObject).getNumber())", target = "number"),
    
Run Code Online (Sandbox Code Playgroud)

java mapping expression mapstruct

5
推荐指数
1
解决办法
4万
查看次数

Protobuf、MapStruct 和 null 值

鉴于这个原型

option java_outer_classname = "FooProto";
message Foo {
    string bar = 1;
}
Run Code Online (Sandbox Code Playgroud)

这个java类:

public class MyFoo {
    String bar;
}
Run Code Online (Sandbox Code Playgroud)

这个映射器(使用 Mapstruct):

@Mapper
public interface FooMapper() {
    FooProto.Foo toProtoFoo(MyFoo myFoo);
}
Run Code Online (Sandbox Code Playgroud)

当我有一个带有空栏的 MyFoo 实例并尝试将其映射到原型时,我得到一个 NullPointerException。

这是因为映射器自动生成的代码调用原型的自动生成方法,如下所示:

public Builder setBar(java.lang.String value) {
    if (value == null) {
        throw new NullPointerException();
    }

    bar_ = value;
    onChanged();
    return this;
}
Run Code Online (Sandbox Code Playgroud)

有什么办法可以避免这个问题吗?(这不涉及在映射之前清理 MyFoo 实例,使其没有空值)

java protocol-buffers mapstruct

5
推荐指数
1
解决办法
2440
查看次数

在 Quarkus 下将 MapStruct 与 Lombok 一起使用

我正在遵循MapStruct 博客上的指南,但在同时使用这 3 种技术时遇到问题。我一直在尝试来自 MapStruct 文档、错误报告、此处帖子的几种方法,但在每种情况下,我最终都会在构建过程中收到以下异常。

有人在 Quarkus 下成功使用 MapStruct 和 Lombok 吗?任何帮助表示赞赏。

compile奇怪的是, a 后的第一个mvn clean总是成功,而第二个compile或运行应用程序会抛出此错误:

Error:(9,8) java: Internal error in the mapping processor: java.lang.RuntimeException:
javax.annotation.processing.FilerException: Attempt to recreate a file for type com.example.service.RawContentDtoMapperImpl
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:59)
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:39)
...
Run Code Online (Sandbox Code Playgroud)

映射器配置:

Error:(9,8) java: Internal error in the mapping processor: java.lang.RuntimeException:
javax.annotation.processing.FilerException: Attempt to recreate a file for type com.example.service.RawContentDtoMapperImpl
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.createSourceFile(MapperRenderingProcessor.java:59)
at org.mapstruct.ap.internal.processor.MapperRenderingProcessor.writeToSourceFile(MapperRenderingProcessor.java:39)
...
Run Code Online (Sandbox Code Playgroud)

映射器:

@MapperConfig(componentModel = "cdi")
    public interface QuarkusMappingConfig { …
Run Code Online (Sandbox Code Playgroud)

java lombok mapstruct quarkus

5
推荐指数
2
解决办法
7104
查看次数

如何配置 MapStruct 在枚举值无法映射时抛出异常

这是我的映射器:

@Mapper
public interface ProductMapper {
    ProductClassification toProductClassification(ProductTypes pisType);
}
Run Code Online (Sandbox Code Playgroud)

其中ProductTypesProductClassification是枚举。我希望它在无法映射枚举时抛出异常,但出现编译器错误: The following constants from the source enum have no corresponding constant in the target enum and must be be mapped via adding additional mappings: EXTERNAL, UNKNOWN.

我尝试使用@ValueMappings注释,但只能将其配置为将值设置为 null,这是不够的:

@ValueMappings({
    @ValueMapping(source = MappingConstants.ANY_REMAINING, target = MappingConstants.NULL)
})
Run Code Online (Sandbox Code Playgroud)

将 MapStruct 映射器配置为在无法映射枚举常量时抛出异常的正确方法是什么?

java enums mapstruct

5
推荐指数
1
解决办法
7528
查看次数

SpringBoot + Lombok + MapStruct 不能一起工作

我正在尝试使用 Lombok 和 MapStruct 配置 SpringBoot (v2.6.2),已经配置了 Maven 编译器插件和 lombok-mapstruct-binding (annotationProcessorPaths),但没有创建 lombok 类:

pom.xml

*<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.knowledge</groupId>
    <artifactId>knowledge</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>knowledge</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <version.mapstruct>1.4.1.Final</version.mapstruct>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${version.mapstruct}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude> …
Run Code Online (Sandbox Code Playgroud)

lombok spring-boot mapstruct

5
推荐指数
1
解决办法
9056
查看次数

无法映射属性。考虑声明/实现映射方法。无法生成从不可迭代类型到可迭代类型的映射方法

我似乎无法正确使用mapstruct

@Mapping(target = "products", source = "itemBookType")
SearchBookingResult backToTp(ItemBook itemBook);
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我收到以下错误:

Can't map property "ProductType itemBookType" to "List<ProductOverview> products". Consider to declare/implement a mapping method: "List<ProductOverview> map(ProductType value)".
Run Code Online (Sandbox Code Playgroud)

我在底部添加了以下代码:

List<ProductOverview> map(ProductType value);
Run Code Online (Sandbox Code Playgroud)

但它仍然返回以下错误:

Can't generate mapping method from non-iterable type to iterable type from java stdlib.
Run Code Online (Sandbox Code Playgroud)

物品簿类别:

public class ItemBook {
    private ProductType itemBookType; //ProductType class
    private Integer idref;
    private String reference;
}
Run Code Online (Sandbox Code Playgroud)

产品类型类别:

public enum ProductType {
    BOOK, PHONE, GAME
}
Run Code Online (Sandbox Code Playgroud)

搜索预订结果类:

public class SearchBookingResult extends BaseResponse<SearchBookingResult> {
    private …
Run Code Online (Sandbox Code Playgroud)

java mapping spring spring-boot mapstruct

5
推荐指数
1
解决办法
2万
查看次数