SpringBoot and Gradle. Get started in 1 min:
Published in
2 min readDec 10, 2018
Dir structure of TestRepo:
├── build.gradle
└── src
└── main
└── java
└── hello
├── Application.java
├── Greeting.java
└── GreetingController.java
build.gradle (to config build)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'bootJar {
baseName = 'hello-greeter'
version = '0.1.0'
}repositories {
mavenCentral()
}sourceCompatibility = 1.8
targetCompatibility = 1.8dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Application.java (Spring boot app. PSVM. Entry point)
package hello;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Greeting.java (For representation)
package hello;public class Greeting { private final long id;
private final String content; public Greeting(long id, String content) {
this.id = id;
this.content = content;
} public long getId() {
return id;
} public String getContent() {
return content;
}
}
GreetingController.java (For handling route. Controller.)
package hello;import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class GreetingController { private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
After setting up your App entry point, your Greeting class and its conroller, we run:
gradle build
That results in:
build
├── classes
│ └── java
│ └── main
│ └── hello
│ ├── Application.class
│ ├── Greeting.class
│ └── GreetingController.class
├── libs
│ └── gs-rest-service-0.1.0.jar
└── tmp
├── bootJar
│ └── MANIFEST.MF
└── compileJava
Now run java -jar build/libs/gs-rest-service-0.1.0.jar
. Now test http://localhost:8080/greeting
.
Cheers! PS: gist