SpringBoot 3.4がGAされました。
Release Notesを参考に3.3系からの変更点をまとめます。
自分が移行するときに関係のあるものや興味があるものが中心となりますがご容赦ください🙇♂️
関連記事はこちら
【要対応】Gradle要件の変更
gradleのバージョンが7系であれば7.6.4以降、8系であれば8.4以降が必要になります。
プロジェクトのgradleバージョンの更新は、以下のコマンドで実行可能です!
※執筆時点の最新が8.10.2
# メジャーバージョンを上げる場合は事前に非推奨な記述がないかチェック
$ ./gradlew build -x check -x test --warning-mode=fail
# バージョン更新
$ ./gradlew wrapper --gradle-version=8.10.2【デグレの可能性】ConfigurationPropertisのバリデート範囲の変更
以下のようなネストされたpropertiesクラスがあるとします。
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "sample")
public class SampleProperties {
@Pattern(regexp = "main")
private String value;
private SubProperties sub;
@Data
public static class SubProperties {
@Pattern(regexp = "sub")
private String value;
}
}3.3までは親クラスにさえ @Validatedを付与していれば、ネストしたクラスもバリデートが実施されました。
3.4からは明示的に 以下のように @Validを付与しなければネストクラスのバリデートが効かなくなります。
@Data
@Validated
@Component
@ConfigurationProperties(prefix = "sample")
public class SampleProperties {
@Pattern(regexp = "main")
private String value;
@Valid // !! TODO !! ネストクラスのフィールドにアノテーションを追加
private SubProperties sub;
@Data
public static class SubProperties {
@Pattern(regexp = "sub")
private String value;
}
}【要対応】@MockBeanと@SpyBeanが非推奨に
@MockBeanと@SpyBeanの代わりに@MockitoBean や @MockitoSpyBean を利用する必要があります。
以下の2パターンでは@MockBeanと同じようにMock化ができました
// ①:@ComponentでBean化
@Component
public class SampleClient {
public String hello() {
return "Hello";
}
}
// ②:@BeanでBean化
@Configuration
public class SampleAutoConfiguration {
@Bean
public SampleClient sampleClient() {
return new SampleClient();
}
}【新機能】MockMvcTester
これまで MockMvcTest というテスト向けのクラスがありましたが、このサンプルのように
@WebMvcTest(SampleController.class)
class Before {
@Autowired
private MockMvc mockMvc;
@Test
void test() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/mockMvcTest/sample"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json("""
{
"message": "Hello, World!",
"detail": {
"key1": "value1",
"key2": "value2"
}
}
"""));
}
}リクエストを送信する時、検証する時、どちらも MockMvcRequestBuildersや MockMvcResultMatchersを意識して実装する必要がありました。
(ちなみに僕は覚えられず毎回どこかからコピペするか自分のブログを見直すかしてました。。。)
そんな課題を解決してくれるのが MockMvcTesterです!
上記と同じ内容をMockMvcTesterで実装するとこうなります。
class After {
// MockMvcTesterの初期化
private final MockMvcTester mockMvc = MockMvcTester.of(new SampleController());
@Test
void test() {
mockMvc.get()
.uri("/mockMvcTester/sample")
.assertThat()
.hasStatusOk()
.bodyJson().isLenientlyEqualTo("""
{
"message": "Hello, World!",
"detail": {
"key1": "value1",
"key2": "value2"
}
}
""");
}
}すべて単純なメソッドチェーンで記述できてめっちゃスッキリ🤩
【改善】DockerCompose利用時に起動・終了時引数を指定可能に
spring.docker.compose.start.argumentsとspring.docker.compose.stop.argumentsによってdocker composeの起動・終了時の引数の指定が可能になりました。
【新機能】SSLbundle利用時に証明書の情報をactuatorから取得可能に
以下のようにssl bundleを利用している場合、/actuator/healthで証明書の情報が取得可能になりました。
spring:
ssl:
bundle:
jks:
server:
key:
alias: "sample"
keystore:
location: classpath:ssl/keystore.p12
password: "sample"
type: "PKCS12"
# 証明書の情報を取得するために必要な設定
management:
endpoints:
web:
exposure:
include: info,health # infoを含める
endpoint:
info:
access: read-only
info:
ssl:
enabled: true実際に取得できる情報はこちらです(証明書はローカルで適当に作ったものです。)
{
"ssl": {
"bundles": [
{
"name": "server",
"certificateChains": [
{
"alias": "sample",
"certificates": [
{
"subject": "O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
"version": "V3",
"issuer": "CN=testca,O=Internet Widgits Pty Ltd,ST=Some-State,C=AU",
"validity": {
"status": "VALID"
},
"validityStarts": "2024-11-20T07:08:08Z",
"validityEnds": "2034-11-18T07:08:08Z",
"serialNumber": "696c6e874e7ba1415ca80779239b56740d77291f",
"signatureAlgorithmName": "SHA256withRSA"
}
]
}
]
}
]
}
}アプリケーション自体の証明書はcurl等で叩けばわかりますが、内部で外部リソースと通信するときの証明書は見れなかったのでありがたい機能ですね 🤔
【新機能】ScheduledTaskの情報が取得可能に
/actuator/scheduledtasksからScheduledTaskの情報が取得できるようになりました。
実際に取得してみたサンプルはこちら
{
"cron": [],
"fixedDelay": [],
"fixedRate": [
{
"runnable": {
"target": "hirabay.migration.scheduledtask.ScheduledTaskSample.sampleTask"
},
"initialDelay": 0,
"interval": 10000,
"lastExecution": {
"time": "2024-11-20T07:38:25.785525Z",
"status": "SUCCESS"
},
"nextExecution": {
"time": "2024-11-20T07:38:35.780875Z"
}
}
],
"custom": []
}その他
Kubernetes環境で LivenessとReadiness エンドポイントがデフォルトで有効に
kubernetes環境と書きましたが、正しくは「Cloud Foundry Platforms」環境で/actuator/health/liveness と /actuator/health/readiness がデフォルトで有効になります。
もともと有効にしていなければ、kubernetesのマニフェストにも上記エンドポイントを設定していないはずなので既存影響とかはないはず
RestTemplateBuilderの一部メソッドが非推奨に
気付いたベースで
return new RestTemplateBuilder()
// befoer
.setConnectTimeout(Duration.ofMillis(500))
// after
.connectTimeout(Duration.ofMillis(500))
.build();
