Reputation: 9279
I have a spring data rest service, that expose a resource like:
@Entity
public class Resource{
private String name;
@Lob
private byte[] data;
private String contentType;
}
How should be a json to insert a resource of this type?
Upvotes: 2
Views: 1175
Reputation: 2479
Spring Content was designed for exactly this.
Assuming you are using Spring Boot then you can add LOB handling as follows:
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>
Add a Store:
@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> {}
}
Associate content with your entity entity:
@Entity
public class Resource {
private String name;
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
@MimeType
private String mimeType = "text/plain";
}
That's all that you should need. When you application starts Spring Content will see the dependencies on the Spring Content JPA/REST modules and it will inject an implementation of the ResourceContentStore
store for JPA as well as an implementation of a controller (at /resourceContent
) that supports that maps GET, POST, PUT and DELETE requests onto the underlying Store interface. The REST endpoint will be available under.
i.e.
curl -X PUT /resourceContent/{resourceId}
will create or update an resource's content
curl -X GET /resourceContent/{resourceId}
will fetch the resource's content
curl -X DELETE /resourceContent/{resourceId}
will delete the resources content
There are a couple of getting started guides here. They use Spring Content for the Filesystem but the modules are interchangeable. The JPA reference guide is here. And there is a tutorial video here.
HTH
Upvotes: 1
Reputation: 908
You don't need JSON. "name" and "contentType" are part of the http header (respectively "Content-Type" and "Content-Disposition: filename") "data" is the HTTP body. Its encoding depends of "Content-Encoding" Maybe you should use "ResourceResolvers" plugged with JPA.
Upvotes: 1
Reputation: 48287
AFAIK, SDR does not handle multipart requests or responses yet, as it can only do JSON.
You can run SDR at the same time as a regular Spring MVC servlet (it's one line of code in your config).
I would suggest using a regular Spring MVC controller for your file upload/download, and SDR for the rest (pun intended).
Upvotes: 2