This is a tutorial on how to configure Spring Data ArangoDB, without using Spring Boot Starter ArangoDB. A more extensive demo about the features of Spring Data ArangoDB can be found in the Spring Boot Starter ArangoDB Demo.
This tutorial is about how to configure Spring Data ArangoDB without using Spring Boot Starter ArangoDB.
For a more extensive tutorial about the features of Spring Data ArangoDB and Spring Boot support, see the Spring Boot Starter documentation.
Set up a project and add every needed dependency. This demo uses Maven and Spring Boot.
Create a Maven pom.xml
:
<?xml version="1.0" encoding="UTF-8"?> <projectxmlns="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> <parent> <relativePath/> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.0</version> </parent> <groupId>com.arangodb</groupId> <artifactId>spring-data-arangodb-tutorial</artifactId> <version>1.0.0</version> <name>spring-data-arangodb-tutorial</name> <description>ArangoDB Spring Data Tutorial</description> <properties> <java.version>21</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-spring-data</artifactId> <version>4.5.0</version> </dependency> </dependencies> </project>
Substitute the versions with the latest available versions that are compatible. See the Supported versions for details.
For this tutorial we will model our entity with a Java record class:
@Document("characters") publicrecordCharacter( @IdStringid, Stringname, Stringsurname ) { }
Now that we have our data model, we want to store data. For this, we create a repository interface which extends ArangoRepository
. This gives us access to CRUD operations, paging, and query by example mechanics.
publicinterfaceCharacterRepositoryextendsArangoRepository<Character, String> { }
We need a configuration class to set up everything to connect to our ArangoDB instance and to declare that all needed Spring Beans are processed by the Spring container.
@EnableArangoRepositories
: Defines where Spring can find your repositoriesarango()
: Method to configure the connection to the ArangoDB instancedatabase()
: Method to define the database namereturnOriginalEntities()
: Method to configures the behavior of repository save methods to either return the
original entities (updated where possible) or new ones. Set tofalse
to use java records.
@Configuration@EnableArangoRepositories(basePackages = {"com.arangodb.spring.demo"}) publicclassAdbConfigimplementsArangoConfiguration { @OverridepublicArangoDB.Builderarango() { returnnewArangoDB.Builder() .host("localhost", 8529) .user("root") .password("test"); } @OverridepublicStringdatabase() { return"spring-demo"; } @OverridepublicbooleanreturnOriginalEntities() { returnfalse; } }
Note that, in case the driver is configured to use a protocol with VPACK
content type (i.e. HTTP_VPACK
or HTTP2_VPACK
), then the ArangoConfiguration#contentType()
method must be overridden to return ContentType.VPACK
as shown in the following example:
@OverridepublicArangoDB.Builderarango() { newArangoDB.Builder() // ... .protocol(Protocol.HTTP2_VPACK); } @OverridepublicContentTypecontentType() { returnContentType.VPACK; }
To run our demo as command line application, we have to create a class implementing CommandLineRunner
:
@ComponentScan("com.arangodb.spring.demo") publicclassCrudRunnerimplementsCommandLineRunner { @AutowiredprivateArangoOperationsoperations; @AutowiredprivateCharacterRepositoryrepository; @Overridepublicvoidrun(String... args) { // first drop the database so that we can run this multiple times with the same datasetoperations.dropDatabase(); System.out.println("# CRUD operations"); // save a single entity in the database// there is no need of creating the collection first. This happen automaticallyCharacternedStark = newCharacter(null, "Ned", "Stark"); Charactersaved = repository.save(nedStark); System.out.println("Ned Stark saved in the database: " + saved); } }
Finally, we create a main class:
@SpringBootApplicationpublicclassDemoApplication { publicstaticvoidmain(finalString... args) { System.exit(SpringApplication.exit( SpringApplication.run(CrudRunner.class, args) )); } }
And run it with:
mvn spring-boot:run
This should produce a console output similar to:
Ned Stark saved in the database: Character[id=2029, name=Ned, surname=Stark]