Build a Java Web Application Using GraphQL API and Spring Boot
Spring Boot is an open-source framework that allows Java developers to create production-grade Spring applications.
This step-by-step guide details how to create a Java sample app via Spring Boot. This app, powered by Contentstack’s GraphQL API, fetches content stored in Contentstack and displays it on your application.
Screenshots
Warning: This sample app is no longer maintained. It remains available for reference. If you have questions regarding this, please reach out to our support team and we will do our best to help!
Prerequisites
- A text editor or an IDE -- IntelliJ IDEA, Spring Tool Suite (STS) or Visual Studio Code
- JDK 1.8 or later
- Gradle 4+ or Maven 3.2+
- Contentstack account
Note: We have assumed that you are familiar with Contentstack and the Spring Boot framework for this tutorial. If not, refer to the docs (Contentstack and Spring) for more details.
Set Up Your App
Through this guide, you’ll learn how to:
If you want to learn how to build a project from scratch, refer to the Build app from Scratch section.
Create a Stack
A stack holds all the data (entries and assets) that you would need to create a website. Log in to your Contentstack account, and create a new stack.
Note down the stack API key as it will be required in the further steps.
Import Content
For quick setup, we have already created a zip file that contains the required content types, content (entries and assets), languages, and environments that you need to get the website up and running.
You can directly import these resources into your stack by using our command-line interface (CLI):
- Install the CLI by running the following command in your terminal (command prompt):
npm install -g @contentstack/cli
- Log in to your Contentstack account:
csdx auth:login
- Download the content zip file, extract it, and note down its path to use in the next step.
- In the Import content command, pass the stack’s API key and the content location (noted in the above step) to import the content:
csdx cm:stacks:import --stack-api-key <stack_ApiKey> --data-dir <path_of_folder_where_content_is_stored>
For example:
csdx cm:stacks:import --stack-api-key bltw2******** --data-dir "C:\Users\Username\Desktop\cli\content"
This command will import all the required components into your stack. Moreover, it will auto-publish the content to all the environments.
- Install the CLI by running the following command in your terminal (command prompt):
Create Delivery Token
A delivery token lets you fetch the published content of an environment. You can create a delivery token for the “development” environment for testing that we will use in the next step.
Later, while executing this app, you can create tokens for other environments.
Build and Configure the App
You have two ways to get started with the app:
- Build App from Scratch (optional)
- Configure Prebuilt App
Build App from Scratch
This is an optional step.
If you want to build your web project from scratch, go to Spring Initializr, enter your project details, and add the following dependencies:
- Spring Web
- Thymeleaf
- Spring Boot DevTools
- Lombok
- Rest Repositories
- Spring Web Services
- Validation
Clicking on Generate will download the web-application.zip file.
You can now start writing the code in the web-application > src > main > java > com > contentstack > webapplication > MainApp.java file of the project.
Then, run the application by following the execution steps mentioned in the Configure Prebuilt App step.
Configure Prebuilt App
We have created a sample app with the required details for quick integration.
You’ll get the zip file of the project. Unzip it and perform the following steps:
- Open the project in any code editor or IDE of your choice and go to the src > main > java > com.contentstack.graphqlspring > MainApp file.
- In this file, there are three instances for each GraphQL function: products, about, and contact.
In these instances, set the properties as mentioned below:
- setURL: Mention the stack’s API key and environment name where content is published.
- setHeader: Mention the delivery token of the environment
For example, refer to the syntax of products function:
GraphqlBuilder gqlInstance = GraphqlBuilder.Builder.newInstance() .setURL("https://graphql.contentstack.com/stacks/<stack_api_key>?environment=<environment_name>") .setQueryString("{ all_product { items { title description } } }") .setHeader("<delivery_token>") .build()
Likewise, refer to this syntax for about and contact instances.
- Finally, run the application by following any of the following methods:
Method 1: Using IDE
If you’re using IntelliJ IDE, right-click on the interface, and click on Run GQLTestApplication.main.
After the app debugs successfully, you can view the website at http://localhost:8080.
- Open the terminal and point it to the project’s root directory.
- Then, run either of the following commands in the terminal:
- Using Gradle:
./gradlew bootRun
- Using Maven:
./mvnw spring-boot:run
- Using Gradle:
- Open another terminal that points to this project’s root directory and run this command:
curl localhost:8080
Method 2: Using terminal (command prompt)
Additional Resource: You can also use the Contentstack Java SDK and Spring Boot to build a web application. Read our Build a Web Application Using Contentstack Java SDK and Spring Boot guide for more details.