This example assumes you have already installed Java and Gradle.
Use the following project structure:
src/
main/
java/
com/
example/
Application.java
build.gradle
build.gradle
is your build script for Gradle build system with the following content:
buildscript {
ext {
//Always replace with latest version available at http://projects.spring.io/spring-boot/#quick-start
springBootVersion = '1.5.6.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
repositories {
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
}
Application.java
is the main class of the Spring Boot web application:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@RequestMapping("/hello")
private String hello() {
return "Hello World!";
}
}
Now you can run the Spring Boot web application with
gradle bootRun
and access the published HTTP endpoint either using curl
curl http://localhost:8080/hello
or your browser by opening localhost:8080/hello.