Creating REST API

We are going to create a simple REST API web application using Spring 5.

Lets first create build.gradle file in the project root directory.

buildscript {
  ext {
    springBootVersion = '2.0.0.M7'
  }
  repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
  mavenCentral()
  maven { url "https://repo.spring.io/snapshot" }
  maven { url "https://repo.spring.io/milestone" }
}


dependencies {
  compile('org.springframework.boot:spring-boot-starter-web')
  testCompile('org.springframework.boot:spring-boot-starter-test')
}

Now lets create the application that will be used to start up our server.

The last step is to create REST API controller that will respond to user requests.

Now we can build the code. Run this command in the project root, where the build.gradle file is located.

Then we have couple of ways to run the application. If we want to run the application for development, we can use Gradle task bootRun.

If we want to see all the tasks available, run gradle tasks --all

If we want to run the application without Gradle, we can use java -jar from the command line.

Check Docker Handbook to see how to run Java application in Docker.

After the application is started up, we can try to call the api from the command line.

Test the application is able to start

Last updated

Was this helpful?