【问题标题】:Spring Data Rest Jpa insert @Lob fieldSpring Data Rest Jpa 插入 @Lob 字段
【发布时间】:2014-03-10 21:59:20
【问题描述】:

我有一个 spring 数据休息服务,它公开如下资源:

@Entity
public class Resource{
    private String name;
    @Lob
    private byte[] data;

    private String contentType;
}

json应该如何插入这种类型的资源?

【问题讨论】:

  • 尝试在您的单元测试中序列化为 Json 以查看其外观或是否可以使其正常工作。
  • 嗯,它似乎是一个 base64 字符串...我会尝试.. thx

标签: spring-data-rest


【解决方案1】:

AFAIK,SDR 尚未处理多部分请求或响应,因为它只能处理 JSON。

您可以与常规 Spring MVC servlet 同时运行 SDR(这是您配置中的一行代码)。

我建议您使用常规 Spring MVC 控制器进行文件上传/下载,其余部分使用 SDR(双关语)。

【讨论】:

  • 我在哪里可以阅读更多关于您所说的 Spring MVC servlet 的信息?
  • @rascio 您将设置一个普通的 spring mvc servlet(参见 mkyong.com),然后将 添加到你的 spring-mvc-servlet.xml 配置
  • 啊……我还以为是不一样的东西啊啊啊
【解决方案2】:

您不需要 JSON。 "name" 和 "contentType" 是 http 标头的一部分(分别为 "Content-Type" 和 "Content-Disposition: filename") “数据”是 HTTP 正文。它的编码取决于“内容编码” 也许您应该使用 JPA 插入的“ResourceResolvers”。

【讨论】:

    【解决方案3】:

    Spring Content 正是为此而设计的。

    假设您使用的是 Spring Boot,那么您可以按如下方式添加 LOB 处理:

    pom.xml

    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-jpa-boot-starter</artifactId>
        <version>0.0.11</version>
    </dependency>
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-rest-boot-starter</artifactId>
        <version>0.0.11</version>
    </dependency>
    

    添加商店:

    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @StoreRestResource(path="resourceContent")
        public interface ResourceContentStore extends ContentStore<Resource,String> {}
    }
    

    将内容与您的实体实体相关联:

    @Entity
    public class Resource {
    
      private String name;
    
      @ContentId
      private String contentId;
    
      @ContentLength 
      private long contentLength = 0L;
    
      @MimeType
      private String mimeType = "text/plain";
    }
    

    这就是您所需要的。当您的应用程序启动时,Spring Content 将看到 Spring Content JPA/REST 模块上的依赖关系,它将为 JPA 注入 ResourceContentStore 存储的实现以及支持该映射的控制器(/resourceContent)的实现GET、POST、PUT 和 DELETE 请求到底层 Store 接口。 REST 端点将在下面可用。

    curl -X PUT /resourceContent/{resourceId} 将创建或更新资源的内容

    curl -X GET /resourceContent/{resourceId} 将获取资源的内容

    curl -X DELETE /resourceContent/{resourceId}会删除资源内容

    有一些入门指南here。他们将 Spring Content 用于文件系统,但模块是可互换的。 JPA 参考指南是here。还有教程视频here

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2015-07-14
      • 2018-01-23
      • 2016-10-10
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多