【问题标题】:Ignore DTO field using MapStruct (Spring boot)使用 MapStruct 忽略 DTO 字段(Spring boot)
【发布时间】:2021-08-18 08:36:47
【问题描述】:

我创建了几个 DTO 和一个 MapStruct 接口来获取用户数据:

public class UserDto {
    private Long id;        
    private CountryDto country;
}

public class CountryDto {
    private Long id;
    private String name;
    
    private List<TimeZoneDto> timeZones = new ArrayList<TimeZoneDto>();
}

@Mapper
public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    // TODO exclude country timezones
    UserDto mapToDto(User entity);
}

我想修改 UserMapper 以便排除 CountryDto 时区列表

{
   "id":1,
   "country": {
        "id": 182,
        "name":"Australia"
   }
}

【问题讨论】:

    标签: java spring-boot dto mapstruct


    【解决方案1】:

    我终于找到了解决方案,只需在 UserMapper 中添加以下行就可以了:

    @Mapping(target = "country.timeZones", ignore = true)
    UserDto mapToDto(User entity);
    

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 2022-01-17
      • 2020-04-24
      • 2020-03-07
      • 2015-12-06
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多