#218 - Add GridFS example for Spring Data MongoDB.
Original pull request: #229.
This commit is contained in:
committed by
Mark Paluch
parent
53dd062c98
commit
2c46c8c8bd
@@ -25,13 +25,13 @@ We have separate folders for the samples of individual modules:
|
||||
* `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics.
|
||||
* `fluent-api` - Example project to show the new fluent API (`MongoTemplate`-alternative) to interact with MongoDB.
|
||||
* `geo-json` - Example project showing usage of [GeoJSON](http://geojson.org) with MongoDB.
|
||||
* `gridfs` - Example project showing usage of gridFS with MongoDB.
|
||||
* `java8` - Example of how to use Spring Data MongoDB with Java 8 date time types as well as the usage of `Optional` as return type for repository methods. Note, this project requires to be build with JDK 8.
|
||||
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.
|
||||
* `reactive` - Example project to show reactive template and repository support.
|
||||
* `security` - Example project showing usage of Spring Security with MongoDB.
|
||||
* `text-search` - Example project showing usage of MongoDB text search feature.
|
||||
|
||||
|
||||
## Spring Data REST
|
||||
|
||||
* `headers` - A sample showing the population of HTTP headers and the usage of them to perform conditional `GET` requests.
|
||||
|
||||
39
mongodb/gridfs/README.md
Normal file
39
mongodb/gridfs/README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# Spring Data MongoDB - GridFS example
|
||||
|
||||
This project contains an example of [GridFS](https://docs.mongodb.com/v3.4/core/gridfs/) specific features of Spring Data (MongoDB).
|
||||
|
||||
## Support for storing a file
|
||||
|
||||
Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to store a file.
|
||||
|
||||
```java
|
||||
InputStream is = ...
|
||||
gridFsOperations.store(is, "myFile1.txt");
|
||||
```
|
||||
|
||||
## Support for query a file by name
|
||||
|
||||
Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to query a GridFSDBFile by its filename.
|
||||
|
||||
```java
|
||||
GridFSDBFile gridFsFile = gridFsOperations.findOne(query(whereFilename().is("myFile1.txt")));
|
||||
```
|
||||
|
||||
## Support for storing a file with metadata
|
||||
|
||||
Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to store a file with additional metadata..
|
||||
|
||||
```java
|
||||
InputStream is = ...
|
||||
Customer customerMetaData = new Customer("Hardy", "Lang");
|
||||
gridFsOperations.store(is, "myCustomerFile.txt", customerMetaData);
|
||||
|
||||
```
|
||||
|
||||
## Support for query a file by metadata
|
||||
|
||||
Using [GridFsOperations](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/gridfs/GridFsOperations.html) to query a GridFSDBFile by metadata.
|
||||
|
||||
```java
|
||||
GridFSDBFile gridFsFile = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy")));
|
||||
```
|
||||
16
mongodb/gridfs/pom.xml
Normal file
16
mongodb/gridfs/pom.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-data-mongodb-gridfs</artifactId>
|
||||
|
||||
<name>Spring Data MongoDB - GridFs</name>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.mongodb.gridfs;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
public String id;
|
||||
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
|
||||
public Customer() {}
|
||||
|
||||
public Customer(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"Customer[id=%s, firstName='%s', lastName='%s']",
|
||||
id, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example.springdata.mongodb.gridfs;
|
||||
|
||||
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsOperations;
|
||||
import org.springframework.data.mongodb.gridfs.GridFsResource;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Query.query;
|
||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;
|
||||
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereMetaData;
|
||||
|
||||
/**
|
||||
* Spring Boot sample application to show the usage of {@link GridFsOperations} with Spring Data MongoDB.
|
||||
*
|
||||
* @author Hartmut Lang
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class GridFsApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private GridFsOperations gridFsOperations;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GridFsApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... strings) throws Exception {
|
||||
|
||||
// store file
|
||||
try (InputStream is = new BufferedInputStream(new ClassPathResource("./myFile1.txt").getInputStream())) {
|
||||
gridFsOperations.store(is, "myFile1.txt");
|
||||
}
|
||||
|
||||
// get file or resource by filename
|
||||
GridFSFile gridFsFile1 = gridFsOperations.findOne(query(whereFilename().is("myFile1.txt")));
|
||||
System.out.println("Filename: " + gridFsFile1.getFilename() +", MD5: " + gridFsFile1.getMD5());
|
||||
GridFsResource gridRes1 = gridFsOperations.getResource("myFile1.txt");
|
||||
StreamUtils.copy(gridRes1.getInputStream(), System.out);
|
||||
|
||||
// store file with metaData
|
||||
try (InputStream is = new BufferedInputStream(new ClassPathResource("./myCustomerFile.txt").getInputStream())) {
|
||||
Customer customerMetaData = new Customer("Hardy", "Lang");
|
||||
gridFsOperations.store(is, "myCustomerFile.txt", customerMetaData);
|
||||
}
|
||||
|
||||
// search by metaData
|
||||
GridFSFile gridFsFile2 = gridFsOperations.findOne(query(whereMetaData("firstName").is("Hardy")));
|
||||
System.out.println("Filename: " + gridFsFile2.getFilename() +", MD5: " + gridFsFile2.getMD5());
|
||||
GridFsResource gridRes2 = gridFsOperations.getResource(gridFsFile2.getFilename());
|
||||
StreamUtils.copy(gridRes2.getInputStream(), System.out);
|
||||
}
|
||||
}
|
||||
1
mongodb/gridfs/src/main/resources/application.properties
Normal file
1
mongodb/gridfs/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
#spring.data.mongodb.grid-fs-database=dbfs
|
||||
2
mongodb/gridfs/src/main/resources/myCustomerFile.txt
Normal file
2
mongodb/gridfs/src/main/resources/myCustomerFile.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
This is my example file.
|
||||
It gets added as file to gridFs.
|
||||
1
mongodb/gridfs/src/main/resources/myFile1.txt
Normal file
1
mongodb/gridfs/src/main/resources/myFile1.txt
Normal file
@@ -0,0 +1 @@
|
||||
Just a simple file to add to GridFS
|
||||
@@ -26,6 +26,7 @@
|
||||
<module>query-by-example</module>
|
||||
<module>reactive</module>
|
||||
<module>fluent-api</module>
|
||||
<module>gridfs</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
Reference in New Issue
Block a user