Spring Boot Gradle

Creating Spring Boot Project with Gradle

This tutorial shows you how to build efficiently a sample blog application by combining the power of Spring Boot and Gradle.

​Navigate to Spring Boot Initializr and configure your project as shown in the following screenshot. Ensure you select Gradle as the build tool. Once configured, download the generated project as a zip file and extract it into your workspace.

how to use spring boot initializr


Configure Build

Add the following plugins to your build.gradle file:

plugins {
    id 'eclipse'
    id 'org.springframework.boot' version '2.5.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

Then, open your command prompt or terminal in the workspace location and run the following command to build the project:

gradlew clean cleanEclipse build eclipse

This command will download the required dependencies. Upon successful build completion, you'll see the corresponding logs in the command prompt.

Import Project to IDE

Open your Eclipse IDE and import the project by navigating to File > Import... > General > Existing Projects Into workspace > Next >

Select your project directory and click on the 'Finish' button.

create gradle project eclipse
If you encounter any warnings upon opening the project, right-click on the warning and select 'Quick Fix', then click 'Finish'.

Configure Application

Open the build.gradle file and set the main class:

springBoot {
    mainClassName = 'com.e3docs.SpringBootGradleApplication'
}

Create API Examples

Now, let's create some API examples. Create a controller class E3DocsController.java in the controller package and an entity class TestEntity.java in the entity package. Here's the code for these classes:

E3DocsController.java:

package com.e3docs.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.e3docs.entity.TestEntity;
@RestController
@RequestMapping("/e3docs/demo/api")
public class E3DocsController {
    @GetMapping("/get")
    public TestEntity get() {
        TestEntity entity = new TestEntity();
        entity.setString("e3docs");
        entity.setNumber(2024);
        entity.setTrueFalse(true);
        return entity;
    }
    @PostMapping("/post")
    public String post(@RequestBody TestEntity entity) {
        if (null != entity && null != entity.getString()) {
            return entity.toString();
        } else {
            return "Entity is Null!...";
        }
    }
}

TestEntity.java:

package com.e3docs.entity;
public class TestEntity {
    private String string;
    private int number;
    private boolean trueFalse;
    // Getters & Setters
    @Override
    public String toString() {
        return "TestEntity [string=" + string + ", number=" + number + ", trueFalse=" + trueFalse + "]";
    }
}

Build and Run

Run the following command to build the project:

gradlew clean assemble

This command will generate a distribution file in the 'build\distributions' directory. Extract the generated file and execute the 'spring-boot-gradle.bat' file. Once the application runs successfully, you'll observe the logs in the Command Prompt.

Test APIs

Use tools like Postman to test the following APIs:

  • GET: http://localhost:8080/e3docs/demo/api/get
spring get API
  • POST: http://localhost:8080/e3docs/demo/api/post
Srping post API

​Troubleshooting Steps for Spring Boot Gradle Setup Errors

  • ​If you encounter any classpath-related errors with the .bat file, add the following code snippet to your build.gradle file:
startScripts {
    doLast {
        def winScriptFile  = file getWindowsScript()
        winScriptFile.text = winScriptFile.text.replaceAll('set CLASSPATH=.*', 'set CLASSPATH=%APP_HOME%\\\\lib\\\\\\*')
    }
}
  • If you prefer not to include other distributions, add the following code to your build.gradle file:
application {
    mainClass = 'com.e3docs.E3docsApplication'
    applicationDefaultJvmArgs = ['-Dfile.encoding=UTF-8']
    bootDistZip.enabled = false
    bootDistTar.enabled = false
    distTar.enabled = false
    bootJar.enabled = false
    executableDir = "" //if you don't want run script inside bin folder else you can skip this line.
}

This comprehensive guide walks you through the process of creating a Spring Boot project with Gradle, setting up the development environment, coding API examples, building the project, running the application, and testing APIs.