Java Sample App

This demo Java terminal app works using Contentstack’s Java SDK. It uses Contentstack to store and deliver the content of the news app. 


To help you get started, we have provided a Java code snippet that shows how to generate queries and fetch entries from Contentstack.


Steps for Execution

  1. Creating a Java Project
  2. Create a Stack
  3. Add a Publishing Environment
  4. Import Content Type
  5. Add Content
  6. Download the Code and Configure the App
  7. Run the Application
  1. Creating a Java Project

    1. Create a new project under File > New > Project > Java and provide a name for your application, {applicationName}Java_Sample_App_-_New_Project.png
    2. Next, open the project in a code editor or an IDE of your choice, for example, IntelliJ, and open the pom.xml file. You will see the dependency section. You need to add one more dependency in the file as shown below:
    3. Save the file and then follow the instructions given in the "Run the Application" section below:Java_Sample_App_-_Run_JavaNews.png
    You can skip setting up the project (as mentioned above) and create the web app quickly, with a sample app with the required details for quick integration.
  2. Create a Stack

    Log in to your Contentstack account, and create a new stack. This stack will hold all the data specific to your website.

  3. Add a Publishing Environment

    To add an environment in Contentstack:

    1. Navigate to Settings and then Environment.
    2. Click the + New Environment tab.
    3. Provide a suitable name for your environment; say ‘staging’.
    4. Specify the base URL (e.g., ‘http://YourDomainName.com’), and select the language (e.g., English - United States).
    5. Then, click Save.
  4. Import Content Type

    A content type is like the structure or blueprint of a page or a section of your web or mobile property. For this app, we need one content type named News. We need this content type to add the content for our app.

    For quick integration, we have already created the content type. Download the content type and import it to your stack. You can also create your own content type if required. Now that your content type is ready, let’s add some content to your app.

  5. Add Content

    Create and publish entries for the News content type you just downloaded and imported, as discussed in the above step.

    Now that we have created the sample data, it’s time to configure the presentation layer.

  6. Download the Code and Configure the App

    Download and unzip the source repository for this guide, or clone the repository by using the following command:

    git clone https://github.com/contentstack/contentstack-java-news-web-app-example.git

    Once the project has been downloaded, move inside the project directory (cd into contentstack-java-news-web-app-example root directory).

    Then open the MainApp.java file which is inside the \contentstack-java-news-web-app-example\src\main\java\io\contentstack\webapp folder and update your stack credentials (Stack API key, Delivery Token, and Environment name) as follows:

    public static void main(String[] args) throws Exception {
       Stack stack = Contentstack.stack("btl23..........", "cs27..........", "env");
       ContentType contentType = stack.contentType("products");
       Query query = contentType.query();
       query.find(new QueryResultsCallBack() {
       public void onCompletion(ResponseType responseType, 
                               QueryResult queryresult,Error error) {

    Note: The credentials are loaded from the local environment .env file. You need to generate those yourself.

  7. Run the Application

    To run the application, open the downloaded code in any IDE of your choice. For this project, we have used IntelliJ IDE. Once you open the application in IntelliJ, it looks like this:


    Right-click the interface (as shown above), and click Run MainApp.main(). The application runs and notifies that the application can be viewed at port number 8080.

    Alternatively, you can execute the following command from the command prompt of the IDE. To do this, go to the root directory of the application and run the following command:

    If you have selected Gradle while setting up the project:

    ./gradlew bootRun

    If you are using Maven, run the following command:

    ./mvnw spring-boot:run

    Now run the service with curl (in a separate command prompt window), by executing the following command:

    $ curl localhost:8080

    You will see the application running at port 8080 as shown below:


    Additional Resource: You can also use the Contentstack Java SDK to build a news app. Read our Build a news app using Contentstack and Contentstack Java SDK guide for more details.

More resources

Was this article helpful?