Intellij idea 将DTO映射到MapStruct中的最终成员

Intellij idea 将DTO映射到MapStruct中的最终成员,intellij-idea,java-8,mapstruct,Intellij Idea,Java 8,Mapstruct,是否有一种方法可以使用MatStruct映射DTO,MatStruct也有一些最终数据成员,并且不能有默认构造函数,例如: public class TestDto { private final String testName; private int id; private String testCase; public TestDto(String testName) { this.testName = testName; }

是否有一种方法可以使用MatStruct映射DTO,MatStruct也有一些最终数据成员,并且不能有默认构造函数,例如:

public class TestDto {

    private final String testName;
    private int id;
    private String testCase;

    public TestDto(String testName) {
        this.testName = testName;
    }

    public String getTestName() {
        return testName;
    }

    public int getId() {
        return id;
    }

    public String getTestCase() {
        return testCase;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setTestCase(String testCase) {
        this.testCase = testCase;
    }
}
请建议如何使用MapStruct映射此数据

您可以使用它来构造DTO的实例

例如:

@Mapper
public interface MyMapper {

    @ObjectFactory
    default TestDto create() {
        return new TestDto("My Test Name");
    }

    //the rest of the mappings
}
您还可以增强
@ObjectFactory
以接受源参数,该参数可用于构造
TestDto
。您甚至可以使用
@Context
作为对象工厂

注意:您不必将
@ObjectFactory
方法放在同一个映射器中,甚至不必将MapStruct
@Mapper
放在同一个映射器中。您可以将其放入任何类中(或使其成为静态),然后
@Mapper(uses=MyFactory.class)
您可以使用它来构造DTO的实例

例如:

@Mapper
public interface MyMapper {

    @ObjectFactory
    default TestDto create() {
        return new TestDto("My Test Name");
    }

    //the rest of the mappings
}
您还可以增强
@ObjectFactory
以接受源参数,该参数可用于构造
TestDto
。您甚至可以使用
@Context
作为对象工厂

注意:您不必将
@ObjectFactory
方法放在同一个映射器中,甚至不必将MapStruct
@Mapper
放在同一个映射器中。您可以将其放入任何类中(或使其成为静态),然后
@Mapper(uses=MyFactory.class)