mapstructMap集< dto>到集< enum>

mtb9vblg  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(560)

我想把userdto转换成user
用户收件人:

public class UserDto {
   private String username;
   private String password;
   private Set<RoleDto> roles;
   //getters and setters
}

角色:

public class RoleDto {
   private String name;
   //getters and setters
}

用户:

public class User {
   private String username;
   private String password;
   private Set<Role>;
   //getters and setters
}

角色:

public enum Role {
   private String name;
   //getters
}

我的Map器类:

@Mapper(componentModel = "spring")
public interface UserMapper {
    UserDto toDto(User user);
    User fromDto(UserDto userDto); //error here
}

但之后呢 maven install 我有个问题:

Error: java: Role does not have an accessible parameterless constructor.

因为角色是enum,所以无法创建公共构造函数。我试过使用:

@Mapping(source = "roles", targer = "roles")
User fromDto(UserDto userDto);

@Mapping(source = "userDto.roles", targer = "user.roles")
User fromDto(UserDto userDto);

但没有效果。我该怎么修?

mfpqipee

mfpqipee1#

我采纳了@rup的建议,将我的角色类修改为:

public class Role {
   private String name;
   // getters and setters
}

相关问题