Recently I completed a little study incorporating Spring for Stardog into a Spring Boot web app, with successful results. Here's how.
Stardog is a Java based RDF repository server (a.k.a. triple-store and more), which supports the RDF graph data model; SPARQL query language; property graph model and Gremlin graph traversal language; HTTP and SNARL protocols for remote access and control; OWL 2 and user-defined rules for inference and data analytics; virtual graphs; geospatial query answering; and programmatic interaction via several languages and network interfaces. At this point, I don't have a lot of experience with Stardog, but I've been experimenting with it to see what I can learn. As part of my study, I recently incorporated Stardog Spring into a Spring Boot web app - taking notes along the way. Here's my notes on how I got it setup and working successfully.
Download and Install Stardog
For my local development environment on Mac OS, I installed Stardog 5 BETA (Community Edition).
You can download Stardog at www.stardog.com. Once you've downloaded it, unzip the archive to a destination directory. I'm put mine in /Users/cburleson/stardog
Next you need to set the STARDOG_HOME environment variable. You can do this by adding an export line to your .bash_profile.
Add the following line:
Save changes made to .bash_profile by hitting Control+o (that’s an o as in otter), ENTER, then exit out of nano by hitting Control+X.
Note that changes made .bash_profile will require the shell to be restarted or a new shell to spawn.
stardog-license-key.bin into the STARDOG_HOME location. Then you can start the Stardog server to test your installation.
If everything is working properly, you should get the following response.
Note that there is an ERROR in this response, but I think it just has to do with the fact that I'm using the Community edition and not an enterprise edition (see: this support issue).
Still, the server is running and can be accessed in a web browser at
You can also check the server status with the following command:
Now, that you've tested the installation, you can stop the server with the following command.
Add the Stardog Maven Repo to pom.xml
In order to get the required dependencies, you need to add the Stardog public maven repository to your repositories defined in the Maven POM (pom.xml). Here's how that section looks in my file:
Add Stardog Dependencies to pom.xml
Now, we can add the required dependencies. Notice that even though I installed Stardog 5 BETA, I'm using a different version number for various dependencies (still, it works).
Notice that I've got stardog-spring-batch commented in the file, but commented out. I put it in the file incase I decide to use it in the future, but for now, I don't need it.
Create or Edit Spring Application Context File
I prefer using pure Java only configuration for Spring, but I had trouble with this one, so opted to use the XML configuration. I created the following file inside of
Notice that the data importer bean is going to look for an RDF file in the classpath, which should exist in
I got that file from the source project for stardog-spring, which you can find on GitHub at: https://github.com/stardog-union/stardog-spring
You could use any RDF file that you want to have auto-loaded into the Stardog repository.
Make the applicationContext.xml Available to Your Spring Boot App
In order for your Spring Boot app to recognize and load the applicationContext.xml, you'll need to add an annotation to the main application class (the one with a Java main() method)...
The annotation you'll need to add is:
My main application class looks like this:
Create an EmbeddedProvider Java Class
The applicationContext.xml file references an EmbeddedProvider class. There is no EmbeddedProvider exposed by the stardog-spring library, but I found one in the stardog-spring source code at
You'll need to create this class in your project and make sure that you reference the package and class properly for the embeddedProvider bean in your applicationContext.xml file.
Use the Spring for Stardog SnarlTemplate in a Controller
For a quick acid test, I created a simple Spring Controller that gets executed when you hit the path
/test in a web browser.
Here's the simple test controller I created, which logs output from a SPARQL query to the console.
How to Use the SnarlTemplate
The Spring Programming section of the documentation on the Stardog website provides some good information. However, if you want to see some actual code examples, you might want to refer to the TestDataSourceFactory.java class on GitHub. That's where I learned how to execute the query shown in my TestController using a SimpleRowMapper.
Test the App
Now you should be able to run your Spring Boot application and hit the TestController (/test in your browser). When you hit the URL, you should see the following output logged to the console, which shows that you've successfully configured and used Spring for Stardog. As you can see, five triples were returned from the given LIMIT 5 SPARQL query...
In this post, I showed how I used Spring for Stardog in a Spring Boot web app. With some minor variation, these instructions could probably be useful for any Spring app and not just a Spring Boot web app.
There is also some useful information in the QUICKSTART.txt file on GitHub that you might find useful, so be sure to check it out.
Logging with Spring Boot (Cody Burleson's Weblog)
Serve static resources with Spring Boot (Cody Burleson's Weblog)
Use Spring for Stardog in a Spring Boot application (Cody Burleson's Weblog)