Reference page Building a RESTful Web Service
Prepare a configuration file and three java files.
$ tree 
.
├── build.gradle
└── src
    └── main
        └── java
            └── hello
                ├── Application.java
                ├── Greeting.java
                └── GreetingController.java
The following two files are different from those of Building a RESTful Web Service.
build.gradle
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
    }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
    baseName = 'hello-service'
    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')
}
src/main/java/hello/GreetingController.java
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;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class GreetingController {
  private static final String template = "Hello, %s!";
  private final AtomicLong counter = new AtomicLong();
@RequestMapping(value="/greeting",method=RequestMethod.GET)
  public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
              String.format(template, name));
  }
}
$ gradle build
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
A file similar to the following will be created
$ tree
.
├── build
│   ├── classes
│   │   └── java
│   │       └── main
│   │           └── hello
│   │               ├── Application.class
│   │               ├── Greeting.class
│   │               └── GreetingController.class
│   ├── generated
│   │   └── sources
│   │       └── annotationProcessor
│   │           └── java
│   │               └── main
│   ├── libs
│   │   └── hello-service-0.1.0.jar
│   └── tmp
│       ├── bootJar
│       │   └── MANIFEST.MF
│       └── compileJava
├── build.gradle
└── src
    └── main
        └── java
            └── hello
                ├── Application.java
                ├── Greeting.java
                └── GreetingController.java
18 directories, 9 files
$ java -jar build/libs/hello-service-0.1.0.jar

Recommended Posts