Create a REST API in Java platform with minimal installation and with minimal coding using Gradle and Spring Boot.
- Download or Clone the repo
- Run
gradlew run - Access http://localhost:8080/ from your browser.
- Java code
- Add any REST API code to src/main/java/com/springboot/quickstart/SampleController.java
- Configurations for
Springandweb.xmlgo into src/main/java/com/springboot/quickstart/Application.java
- Install Gradle
- Create a folder - say
mkdir quickest-restapi. - Go inside that folder - i.e
cd quickest-restapi - Run
gradle init - You should have a bunch of gradle related files generated in this folder.
- Open
build.gradle- Uncomment the code - i.e. remove
/*&*/comment characters. - Add the below lines into
build.gradlefile-
Add
spring-bootgradle plugin//Step-1: Spring boot plugin apply plugin: 'spring-boot' -
Add dependencies for building the application.
//Scripts/Components to use during Build buildscript { repositories { mavenCentral() } dependencies{ //Step-2: Spring boot plugin-package classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.RELEASE") } } -
Add
compiletime dependencies independencies- not thedependenciesunderbuildscript//Step-3: Spring boot starter web compile("org.springframework.boot:spring-boot-starter-web")
-
- Uncomment the code - i.e. remove
- Create a
Controllerfor REST API endpoints- Refer to the minimally needed code in src/main/java/com/springboot/quickstart/SampleController.java
- Create a MainClass for the kick starting the REST API.
- Run
gradlew run - Access http://localhost:8080/ from your browser.
- Try adding
compile("org.springframework.boot:spring-boot-starter-actuator")for more details about the Web Application you have created- Refer to the log file for more REST endpoints when the actuator is used
- Try adding
compile("org.springframework.boot:spring-boot-starter-data-jpa")andcompile("org.hsqldb:hsqldb:2.3.1")and trial database code. - Try using
apply plugin: 'jetty'and rungradlew jettyRunfor executing the same in Jetty server
