@Mapper(uses = {RoleMapper.class})
public interface UserMapper {
@Mapping(source = "roleIds", target = "roles")
UserFilter toUserFilter(UserFilterDto dto);
}
@Mapper
public interface RoleMapper {
default List<Role> roleIdListToRoleList(List<Long> roleIds) {
return Objects.isNull(roleIds)
? Lists.newArrayList()
: roleIds.stream().map(id -> Role.builder().id(id).build()).collect(Collectors.toList());
}
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserFilterDto extends FilterDto {
@JsonProperty("username")
private String username;
@JsonProperty("isEnabled")
private Boolean isEnabled;
@JsonProperty("roleIds")
private List<Long> roleIds;
}
目标对象
@Data
@Builder
class UserFilter {
private String username;
private Boolean isEnabled;
private List<Role> roles;
}
我希望每个列表
都是列表
的映射,带有rolemapper
,mapstruct生成如下所示:
public UserFilter toUserFilter(UserFilterDto dto) {
UserFilterBuilder userFilter = UserFilter.builder();
userFilter.roles( roleMapper.roleIdListToRoleList(dto.getRoleIds()));
userFilter.username( dto.getUsername() );
userFilter.isEnabled( dto.getIsEnabled() );
return userFilter.build();
}
但相反,我得到了一个编译错误:
源参数中不存在名为“Roleids”的属性。你是说“空”吗?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tradeshare</groupId>
<artifactId>tradeshare</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tradeshare</name>
<description>tradeshare</description>
<properties>
<java.version>11</java.version>
<jsonwebtoken.version>0.9.1</jsonwebtoken.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.4.7</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jsonwebtoken.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>-Amapstruct.defaultComponentModel=spring</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
似乎您使用的是Lombok V.>1.18。如果是这样,你就失踪了
Lombok-Mapstruct-绑定
在POM中添加下一个依赖项:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
我有麻烦映射一个嵌套dto字段正确与MapStruct。我有几个DTO: 具有相应的映射器 到目前为止,一切工作都很好,生成的代码自动连接其他需要的映射器来正确地构建DTO。例如生成的仪器映射器实现 现在,当我试图创建一个包含嵌套工具dto的映射器时遇到了麻烦。映射器应使用instrumentMapper正确创建所需的dto。DTO: 映射器: 生成的代码: 现在media mapper得到了很好
“”“@Data@Builder(toBuilder=true)@allargsconstuctor@noargsconstuctor public class ServiceAddress{private String addLine1;private String addLine2;private String city;private String state;private String
最后,我在我的映射类中使用了这个方法: 但这返回给我。我知道我的代码有问题,但我找不到适合我需要的有用的东西。你能帮我解决这个问题吗?
我尝试使用MapStruct编写映射器类,如下所示: 目前它显示了“未知属性”“customer.customerid”和“usertypes.usertype.userid”等错误。有人能帮我用MapStruct映射所有这些元素吗? 问题2:我们如何绘制跟踪图?1)customerId usertypes->user->userid 2)pdtPrice offers->OffersType->
下面的例子中,我有一个单独的域层和一个单独的持久层。我使用Mapstruct进行映射,当从域映射到实体或从实体映射到域时,会出现堆栈溢出,因为双向引用总是被调用- 用于映射的类非常基本
我想将泛型类型<code>Y</code>的对象映射到另一个泛型类型为<code>X</code>的对象。在mapstruct中有这样的功能吗?或者我必须为通用映射编写自定义映射器吗?当我编译上面的代码时,会出现编译错误。