如何在Spring Data Rest中为实体保存文件? [英] How do you save files for entity in Spring Data Rest?

查看:99
本文介绍了如何在Spring Data Rest中为实体保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SDR的常规实体中,它将保存实体的所有属性,并将其保存到数据库中.但是您如何处理文件?

In a regular entity for SDR, it takes care of all properties of an entity for you saving it to the database. But how do you handle files?

@Entity
public class User {
 String name;
 Set<File> myfiles; //how can I make this work? 
}

@RepositoryRestResource
public interface UserRepository extends JpaRepository<User, Long> {}

如何使用户拥有文件列表,可以上载和下载文件?

How can I make it so that a User owns a list of files, can upload and download them?

推荐答案

Spring Data/REST真正不可能做到这一点,因为它专注于结构化数据.即表格和关联.

This is not really possible with Spring Data/REST as it focusses on structured data; i.e. tables and associations, for the most part.

@Lob 是有问题的,因为它迫使您将内容存储在数据库中,而不一定要存储在数据库中.例如,文件系统或S3可能更好.

@Lob is problematic as it forces you to store your content in the database which isn't necessarily where you want to store it. The file-system or S3 might be better for example.

byte [] 也是有问题的,因为这很可能导致OutOfMemoryExceptions.

byte[] is also problematic if you have very large files as you will likely cause OutOfMemoryExceptions.

相反,有一个名为 Spring Content 的社区项目可以完全解决您的问题正在尝试解决.

Instead, there is a community project called Spring Content that addresses exactly the problem you are trying to solve.

Spring Content为非结构化数据提供与Spring Data/REST相同的编程范例;因此,使用此项目,您可以将一个或多个内容"对象与Spring Data实体相关联,并通过HTTP管理它们,就像您对Spring Data Entities一样.

Spring Content provides the same programming paradigms as Spring Data/REST for unstructured data; i.e. images, documents, movies, etc. So, using this project you can associate one, or in your case, many "content" objects with your Spring Data entities and manage them over HTTP just like you do with your Spring Data Entities too.

添加到您的项目非常简单,如下所示:

Its pretty simple to add to your project, as follows:

pom.xml(也提供启动启动器)

pom.xml (boot starters also available)

   <!-- Java API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-fs</artifactId>
      <version>1.0.0.M4</version>
   </dependency>
   <!-- REST API -->
   <dependency>
      <groupId>com.github.paulcwarren</groupId>
      <artifactId>spring-content-rest</artifactId>
      <version>1.0.0.M4</version>
   </dependency>

配置

@Configuration
@EnableFilesystemStores
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class ContentConfig {

   @Bean
   FileSystemResourceLoader fileSystemResourceLoader() throws IOException {
    return new FileSystemResourceLoader(new File("/path/to/uploaded/files").getAbsolutePath());
   }
}    

要关联内容,请按如下所示修改您的用户实体:

To associate content, modify your User entity as follows:

@Entity
public class User {
 String name;
 List<Image> images; 
}

添加图片实体:

@Entity
public class Image {

  @ContentId
  private String contentId;

  @ContentLength
  private long contentLength = 0L;

  @MimeType
  private String mimeType = "text/plain";
}

并为此添加一个存储"(相当于存储库,但包含内容):

And to this add a "store" (the equivalent of a Repository but for content):

ImageStore.java

ImageStore.java

@StoreRestResource
public interface ImageStore extends FilesystemContentStore<Image, String> {}

这就是创建REST端点@ /users/{userId}/images 的全部.当您的应用程序启动时,Spring Content将查看Spring Content Filesystem并查看您的依赖项,并查看您的 ImageStore 接口,并为该接口注入基于文件系统的实现.它还将看到Spring Content REST依赖关系,并注入一个 @Controller 实现,该实现将HTTP请求转发到您的 ImageStore .就像Spring Data为您的 UserRepository 所做的一样.这省去了您必须自己实现的任何事情,我想这就是您要追求的.

This is all you need to create REST endpoints @ /users/{userId}/images. When your application starts, Spring Content will look at your dependencies seeing Spring Content Filesystem, look at your ImageStore interface and inject a filesystem-based implementation of that interface. It will also see the Spring Content REST dependency and inject an @Controller implementation that forwards HTTP requests to your ImageStore. Just like Spring Data does for your UserRepository. This saves you having to implement any of this yourself which I think is what you are after.

所以...

要使用注入的REST API管理内容,请执行以下操作:

To manage content with the injected REST API:

curl -X POST/users/{userId}/images -F file=@/path/to/image.jpg

curl -X POST /users/{userId}/images -F file=@/path/to/image.jpg

会将图像存储在文件系统上的< 并将其与ID为 userId 的用户实体相关联.

will store the image on the filesystem at `` and associate it with the user entity whose id is userId.

curl/users/{userId}/images/{contentId} -H接受:image/jpeg"

将再次获取它,依此类推...支持所有CRUD方法和视频流以及BTW!

will fetch it again and so on...supports all CRUD methods and video streaming as well BTW!

此处有一些入门指南.Spring Content Filesystem的参考指南是此处.并且在此处中有一个教学视频.编码位大约从1/2开始.

There are a couple of getting started guides here. The reference guide for Spring Content Filesystem is here. And there is a tutorial video here. The coding bit starts about 1/2 way through.

另外两个要点:-如果您使用的是Spring Boot Starters,那么大多数情况下就不需要@Configuration.
-就像Spring Data是抽象的一样,Spring Content也是如此,因此您不仅限于将图像存储在文件系统上.您可以将它们作为BLOB存储在数据库中,也可以存储在S3之类的云存储中.

A couple of additional points: - if you use the Spring Boot Starters then you don't need the @Configuration for the most part.
- Just like Spring Data is an abstraction, so is Spring Content so you aren't limited to storing your images on the filesystem. You could store them as BLOBs in the database, or in cloud storage like S3.

HTH

这篇关于如何在Spring Data Rest中为实体保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆