我正在使用具有默认配置的 MapStruct。除了构建实现(*.java)文件位置之外,一切都按预期工作。应用程序构建后,它们位于包含*.class文件的build/classes/java...包中。如何配置 MapStruct 来避免此类问题?
我在 MapStruct 网站上搜索类似的问题,但没有找到任何内容。
//build.gradle
dependencies {
implementation("org.mapstruct:mapstruct-jdk8:1.2.0.Final")
annotationProcessor("org.mapstruct:mapstruct-processor:1.2.0.Final")
}
Run Code Online (Sandbox Code Playgroud) 我需要将 a 转换dto为entity,并且entity有一个要填充的字段,该字段不需要 的任何字段dto。事实上,该@Mapping注释没有任何来源。
让我们用这个简单的例子来说明它:
public class Employee {
private String firstName;
private String customId;
}
public class EmployeeDto {
private String firstName;
}
Run Code Online (Sandbox Code Playgroud)
customId如您所知,实体的字段Employee不存在于EmployeeDTO.
另外,我还有以下内容formatter。显然,我创建了一个@CustomIdGenerator @interface
public class EmployeeFormatter {
@CustomIdGenerator
public static String simulationIdGenerator() {
return // businessLogic
}
}
Run Code Online (Sandbox Code Playgroud)
最后我的映射器看起来像这样:
@Mapper(uses = EmployeeFormatter.class)
public abstract class EmployeeMapper {
@Mapping(target = "customId", qualifiedBy = customIdGenerator.class)
public abstract Employee toEmployee(EmployeeDTO dto); …Run Code Online (Sandbox Code Playgroud) 我需要一个类的映射结构映射,该类具有要映射到目标类的对象列表,如下所示:
//Source class:
public class VoucherTransaction {
private List<Voucher> vouchers;
}
//TargetClass
public class VoucherTransactionServiceDTO {
private List<UUID> voucherIds;
private List<String> voucherSerials;
}
public class Voucher {
private UUID id;
private String serial;
}
Run Code Online (Sandbox Code Playgroud) 当我将 JSON Lombok @Value DTO 映射到 JPA @Value 实体类(反之亦然)时,编译会引发异常。
“没有可访问的无参数构造函数”
使用Java 11、hibernate、spring boot 5、Lombok 和map struct 1.3.1。
我看到2018年有一个类似的问题: Mapstruct to update value without overwriting,但没有解决这个问题的例子。
因此,我不知道如何解决它。
我正在使用Lombok和MapStruct
UserEntity代表数据库中的表
@Getter
@Setter
@Entity
@Table(name = "users")
public class UserEntity implements Serializable {
private static final long serialVersionUID = -3549451006888843499L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // this specifies that the id will be auto-incremented by the database
private Long id;
@Column( nullable = false)
private String userId;
@Column( nullable = false, length = 50)
private String firstName;
@Column( nullable = false, length = 50)
private String lastName; …Run Code Online (Sandbox Code Playgroud) 我有以下测试:
@SpringBootTest(classes = {SomeService.class, DtoMapperImpl.class})
class SomeServiceTest {
Run Code Online (Sandbox Code Playgroud)
以及以下映射器:
@Mapper(componentModel = "spring")
public interface DtoMapper {
EntityDto toDto(Entity entity);
}
Run Code Online (Sandbox Code Playgroud)
我不会更改包(这意味着DtoMapperImpl与DtoMapper位于同一包中)
一旦我将 Impl 更改为接口,我的测试就会失败:
@SpringBootTest(classes = {SomeService.class, DtoMapper.class})
class SomeServiceTest {
Run Code Online (Sandbox Code Playgroud)
引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“someService”的bean时出错:通过构造函数参数2表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“DtoMapper”类型的合格 bean:预计至少有 1 个符合自动装配候选资格的 bean。依赖注释:{}
您能建议解决这个问题的最佳方法吗?我正在使用 MapStruct 1.3.1.Final
我想在Kotlin中使用Mapstruct将实体映射到具有嵌套 DTO 的 DTO。
我的第一个 DTO 定义如下:
data class FirstDto (
val something: String
)
Run Code Online (Sandbox Code Playgroud)
该 DTO 使用 Mapstruct 映射到实体中,反之亦然。这是映射器:
@Mapper(componentModel = "spring")
interface FirstMapper {
fun entityToDto(entity: FirstEntity): FirstDto
fun dtoToEntity(dto: FirstDto): FirstEntity
}
Run Code Online (Sandbox Code Playgroud)
第二个 DTO 嵌套第一个 DTO:
data class SecondDto (
val somethingElse: String,
val firstDto: FirstDto
)
Run Code Online (Sandbox Code Playgroud)
对于第一个DTO,我使用Mapstruct定义了一个Mapper。但是,我希望这个映射器使用FirstMapper来映射嵌套的 DTO。所以我应该使用Mapperuses的属性。
在 Java 中,这看起来像这样:@Mapper(componentModel = "spring", uses = FirstMapper.class)。
应该如何使用Kotlin来实现?
我正在尝试在 MapStruct 中生成一个实现,它将为我创建一个构造函数,我可以将其用于基于构造函数的依赖项注入。我了解到我不能在映射器定义中使用构造函数注入(见下文),但是如何才能使我生成的类拥有一个构造函数注入?
我已经尝试过以下:
@Mapper(componentModel = "spring", uses = Dependency.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public abstract class MapStructTest {
private Dependency dependency;
@Mapping(source = "field", target "target")
@Mapping(target = "target2", ignore = true)
@AfterMapping
public final void runAfter() {
//dostuff for target2
}
}
Run Code Online (Sandbox Code Playgroud)
没有成功。我的类已生成,看起来不错,但没有构造函数。如何定义我的映射器,以便获得可以在实现中使用的构造函数?
谨致问候,瑞典王子
我正在尝试编写协议缓冲区(gRPC)映射。
我有一个类结构如下
public abstract class A;
public class B extends A;
public class C extends A;
public class Source {
A a1;
A a2;
}
Run Code Online (Sandbox Code Playgroud)
我有一个原始文件,上面写着:
message Target {
oneOf {
B b1 = 1;
C c1 = 2;
}
oneOf {
B b2 = 3;
C c2 = 4;
}
}
Run Code Online (Sandbox Code Playgroud)
AKA,proto 有两个 VALID 字段,但有四个真实字段,source 只有两个字段。而且类型也很复杂。Mapstruct 将无法根据类型签名理解我想要的内容,我需要使用一些限定符来帮助它。
我还设置了它,以便任何未映射的目标属性都是错误。
这让我陷入了尴尬的境地。据我了解,mapstruct 只有源条件。只是存在检查,我不能将其设为“无论我想要什么检查”。
在理想的世界里,我会简单地写:
message Target {
oneOf {
B b1 = 1;
C c1 = 2;
}
oneOf {
B b2 …Run Code Online (Sandbox Code Playgroud) 如何将Stringtruct映射器配置为在将String转换为Long时检查为空为空.
if ( entityOld.getNumber() != null ) {
entityNew.setNumber( Long.parseLong( entityOld.getNumber() ) );
}
Run Code Online (Sandbox Code Playgroud)
我得到的例外是:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_131]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_131]
Run Code Online (Sandbox Code Playgroud)
所以基本上如果一个字符串是空的,我希望它被认为具有值"0".