🌼
SpringBootしてみた
jdk install
macOS/x64をダウンロード
memo
#ダウンロードしたファイルのsha256が正しいことを確認
Downloads $ shasum -a 256 openjdk-17.0.1_macos-x64_bin.tar.gz
6ccb35800e723cabe15af60e67099d1a07c111d2d3208aa75523614dde68bee1 openjdk-17.0.1_macos-x64_bin.tar.gz
Downloads $ cat openjdk-17.0.1_macos-x64_bin.tar.gz.sha256
6ccb35800e723cabe15af60e67099d1a07c111d2d3208aa75523614dde68bee1
Downloads $
jdk install
Downloads $ tar xzvf openjdk-17.0.1_macos-x64_bin.tar.gz
Downloads $ sudo mv jdk-17.0.1.jdk /Library/Java/JavaVirtualMachines
Downloads $ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-39)
OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)
init
spring initializr
と打つと出てくるので、選択していきます
- Spring Boot version 2.6.2
- packageing type Jar
- java version 17
- gradle
- depencies Spring Web,Lombok
作成したプロジェクトでvscodeを開く
プロジェクトフォルダに移動して開き直さないと、vscodeでパッケージ部分に警告が出てしまう。。。
memo
#vscodeをカレントディレクトリで開く
code .
Building a RESTful Web Service
package com.example.restservice;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Greeting {
private final long id;
private final String content;
}
この拡張を入れないとvscodeでlombokが効かない
GreetingController
package com.example.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
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();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
RestserviceApplication
package com.example.restservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestserviceApplication {
public static void main(String[] args) {
SpringApplication.run(RestserviceApplication.class, args);
}
}
build.gradle
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
run
restservice $ ./gradlew bootRun
build
restservice $ ./gradlew build
jarで実行
restservice $ java -jar build/libs/restservice-0.0.1-SNAPSHOT.jar
docker
build/libs/restservice-0.0.1-SNAPSHOT-plain.jar が不要で邪魔なので生成しないようにする
build.gradle追記
jar {
enabled = false
}
restservice $ ./gradlew clean
restservice $ ./gradlew build
Dockerfile
FROM openjdk:17.0.1-slim-buster
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
# イメージ作成
restservice $ docker build -t springboot .
# 起動
restservice $ docker run --rm -d -p 8080:8080 --name spring springboot
restservice $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd630f4123b8 springboot/docker "java -jar /app.jar" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp spring
# ログ確認
restservice $ docker logs spring
restservice $ docker stop spring
link
Discussion