December 11, 2019

Creating a Simple REST API using Java Spring Boot + Gradle + Spring Initializr

We are going to discuss how to set up a simple REST API application using spring boot. Instead of starting a spring boot application from scratch, we will be using spring initializr to bootstrap our application.

Prerequisite :

  • Eclipse
  • Jdk 8
  • Gradle/Maven (usually included with Eclipse, you won’t have to do any other setup

Step 1 : Download a basic spring boot application from start.spring.io

 

Select dependency Spring Web

After downloading the project, extract it and import it to eclipse workspace as Existing Gradle Project

OR

Spring tool suite plugin from eclipse

You can also get a spring boot starter project by installing Spring Tools 3 Plugin from Eclipse Market Place, to directly download the starter project using eclipse (after installation of the plugin, you will have to restart eclipse).
In this case, you can create a new spring starter project directly from eclipse. File-> New -> Other.. -> Spring Starter Project

New Spring starter project using spring tool suite 3
Select Spring Web Dependency and tap Finish

Both cases you will have a working spring boot application.

Step 2 : Create a class Demo Controller

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

	
	@RequestMapping(value="demo",method = RequestMethod.GET)
	public String demo() {
		return "Hello World!";
	}
}

Step 3 : Start the Main class (class having annotation @SpringBootApplication) as Java Application or Spring boot application

Step 4 : Go to your favorite browser and go to link : localhost:8080/demo

Hooray! We created a Hello World Rest API application using Spring boot and Gradle

2 thoughts on “Creating a Simple REST API using Java Spring Boot + Gradle + Spring Initializr

Leave a Reply

Your email address will not be published. Required fields are marked *