Skip to content

Instantly share code, notes, and snippets.

@mayankcpdixit
Last active December 10, 2018 06:36
Show Gist options
  • Save mayankcpdixit/60772b8e7616faa155a96e0820772ab5 to your computer and use it in GitHub Desktop.
Save mayankcpdixit/60772b8e7616faa155a96e0820772ab5 to your computer and use it in GitHub Desktop.
Getting started with SpringBoot and Gradle

Spring boot and gradle. Get started in 1 min:

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.8

dependencies {
    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: blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment