如何在将DTO发送到客户端之前添加自定义逻辑?

时间:2017-05-30 08:15:41

标签: java dto mapstruct

这可能是一个非常简单的问题,但在解决给定问题时我很遗憾。

我有一个名为MediaResource的实体,它有id,imageName等字段,另一方面,MediaResourceDTO有imageName,byte [] resourceFile等。

想法是从数据库中获取图像名称,在将MediaResourceDTO发送到客户端之前,我想将图像文件加载到MediaResourceDTO中。

我不知道如何从MapStruct实现这一目标。

我已经看过@Before和@After的几个例子,但我不了解它的用例。

这是我的源代码: 的 MediaResourceMapper

@Mapper(componentModel = "spring", uses = {VehicleMapper.class, })
public interface MediaResourceMapper {

    MediaResourceDTO mediaResourceToMediaResourceDTO(MediaResource mediaResource);

    List<MediaResourceDTO> mediaResourcesToMediaResourceDTOs(List<MediaResource> mediaResources);

    MediaResource mediaResourceDTOToMediaResource(MediaResourceDTO mediaResourceDTO);

    List<MediaResource> mediaResourceDTOsToMediaResources(List<MediaResourceDTO> mediaResourceDTOs);

    default MediaResource mediaResourceFromId(Long id) {
        if (id == null) {
            return null;
        }
        MediaResource mediaResource = new MediaResource();
        mediaResource.setId(id);
        return mediaResource;
    }
}

MediaResource 实体

@Entity
@Table(name = "media_resource")
public class MediaResource implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "field_name")
    private String fieldName;

    @Column(name = "original_media_file_name")
    private String originalMediaFileName;

    @Column(name = "encrypted_media_file_name")
    private String encryptedMediaFileName;


    @Column(name = "resource_file_content_type", nullable = false)
    private String resourceFileContentType;

}

MediaResourceDTO

public class MediaResourceDTO implements Serializable {

    private Long id;

    private String fieldName;

    private String originalMediaFileName;

    private String encryptedMediaFileName;

    @NotNull
    @Size(max = 20000000)
    @Lob
    private byte[] resourceFile;
    private String resourceFileContentType;

//Some getter and setters


    }

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以customize your mappings提供装饰器。为此,您可以创建自己的类来实现映射器的接口并覆盖所需的映射方法:

public abstract class MediaResourceMapperDecorator implements MediaResourceMapper {

    private final MediaResourceMapper delegate;

    public MediaResourceMapperDecorator(MediaResourceMapper delegate) {
        this.delegate = delegate;
    }

    @Override
    MediaResourceDTO mediaResourceToMediaResourceDTO(MediaResource mediaResource) {
        // Let MapStruct do the basic mapping:
        final MediaResourceDTO dto = this.delegate.mediaResourceToMediaResourceDTO( mediaResource );

        // Then load and add the file to the DTO:
        dto.setResourceFile( loadMyFile( mediaResource.getOriginalMediaFileName() );

        return dto;
    }
}

然后使用mapper上的@DecoratedWith注释来引用你的装饰者:

@Mapper(componentModel = "spring", uses = {VehicleMapper.class, })
@DecoratedWith(MediaResourceMapperDecorator.class)
public interface MediaResourceMapper {
    ...

答案 1 :(得分:1)

您也可以告诉MapStruct它需要哪种服务,以便将filename映射到resourceFileString映射到byte[]

您的服务API:

public interface FileLoader {

    // @Named or some custom annotation annotated with @Qualifier can be
    // used in order to make sure that this will only be applied to the specified mapping
    @Named("fileMapper")
    byte[] loadFile(String filename);
}

你的Mapper看起来像:

@Mapper(componentModel = "spring", uses = {VehicleMapper.class, FileLoader.class })
public interface MediaResourceMapper {

    @Mapping(source = "originalMediaFileName", target = "resoruceFile", qualifiedByName = "fileMapper") // or qualifiedBy = CustomAnnotation.class
    MediaResourceDTO mediaResourceToMediaResourceDTO(MediaResource mediaResource);

   //The rest is the same
}

我个人更喜欢这种方法而不是装饰器,因为你只有一个地方可以定义你的映射,而MapStruct将处理其余的。

有关基于限定符的映射的更多信息,请参阅参考文档中的here