Java 使用JPA,Jersey从数据库检索Blob(pdf)

Java 使用JPA,Jersey从数据库检索Blob(pdf),java,rest,jpa,blob,Java,Rest,Jpa,Blob,我在后端有一个使用JPA-REST的JSP页面,我已经设法将一个blob插入数据库。现在我希望能够从数据库中检索/获取blob,但我似乎找不到任何通过Jersey而不是使用Servlet来实现这一点的示例(我对创建自己的REST服务非常陌生) 这是我用来将blob插入数据库的代码: @POST @Path("upload/{id}") @Consumes({"application/x-www-form-urlencoded", "multipart/form-data"}) public vo

我在后端有一个使用JPA-REST的JSP页面,我已经设法将一个blob插入数据库。现在我希望能够从数据库中检索/获取blob,但我似乎找不到任何通过Jersey而不是使用Servlet来实现这一点的示例(我对创建自己的REST服务非常陌生)

这是我用来将blob插入数据库的代码:

@POST
@Path("upload/{id}")
@Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})
public void addBlob(@PathParam("id") Integer id, @FormDataParam("file") InputStream uploadedInputStream) throws IOException {
    ClientCaseDoc entityToMerge = find(id);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        entityToMerge.setDocument(out.toByteArray());
        super.edit(entityToMerge);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
是否有类似的方法从数据库中检索blob?还是我必须使用servlet


非常感谢您的帮助。

这已经得到了回答,但有助于解决更广泛的问题

我有一个实体

@Entity
public class BlobEntity {

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

    @Column(name = "NAME")
    private String name;

    @Lob
    @Column(name="DATA", length=100000)
    private byte[] data;
JPA存储库

@Repository
public interface BlobEntityRepository extends CrudRepository<BlobEntity, Long> {
}
配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <context:component-scan base-package="com.greg" />
    <tx:annotation-driven />
    <jpa:repositories base-package="com.greg" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:file:~/data/jpa-test" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.greg" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

</beans>

创造
假的
org.hibernate.dial.h2方言
有没有类似的方法从数据库中检索blob?还是我必须使用servlet

虽然问题中没有提到,但我认为这是关于通过Jersey而不是使用servlet返回BLOB的。如果我错了,请在评论中纠正我。如果我是对的,您可能希望更新您的问题,以提及泽西岛

我认为这个问题是重复的。然而,这些评论似乎显示出一些关于如何在老年退休金项目中实施的困惑。当您在域模型中加载PDF时(如果这是件好事,我会让其他人争论),不需要流媒体。您所需要做的就是创建一个集合,将集合作为数据层返回的字节数组

@Path("upload/{id}")
@GET
public Response getPDF(@PathParam("id") Integer id) throws Exception {
    ClientCaseDoc entity = find(id);
    return Response
            .ok()
            .type("application/pdf")
            .entity(entity.getDocument()) // Assumes document is a byte array in the domain object.
            .build();
}

@BorisPavlović老实说,我认为这些答案非常不清楚,其中一个创建了自己的类PDFGenerator,另一个创建了一个QRCode作为PNG,另一个发布了他的代码以导出excel(xlsx),正如我在问题中提到的,我现在不使用servlets。当然,您使用的是servlets。任何处理web请求的服务器端java代码都是servlet。REST使它变得更容易,但它仍然是一个servlet。@BorisPavlović好吧,谢谢你,我现在学到了一些新东西。。在这种情况下,为什么人们在使用REST时创建自己的servlet类?这没有道理……嗯,我不知道你说servlet时是怎么想的……不使用Spring和JPA存储库是否可以做到这一点?我现在拥有的是一个实体类,FacadeREST,pom.xml,AbstractFacade,Application Config,JSP页面。我要做的是在@Path(“Download/{id}”)找到连接到该{id}的blob,并将其作为响应发送回JSP页面。您需要使用JPA(API)和Hibernate(实现非常简单,我添加了spring配置。我使用的是JPA和jersey。我建议您创建一个服务层,它封装了一个存储库层。您可以通过复制测试来创建您的服务。jersey只是做前端所有的请求/响应工作。我展示的所有内容都是JPA。
@Path("upload/{id}")
@GET
public Response getPDF(@PathParam("id") Integer id) throws Exception {
    ClientCaseDoc entity = find(id);
    return Response
            .ok()
            .type("application/pdf")
            .entity(entity.getDocument()) // Assumes document is a byte array in the domain object.
            .build();
}