springboot 2.6がGAとなりました
Release Noteを参考に2.5系からの変更点をまとめます
自分が移行するときに関係のあるものや興味があるものが中心となりますがご容赦ください🙇♂️
関連記事はこちら
Minimum Requirements Changes
変更なしとのことなので、
既存プロジェクトのGradleやJavaのバージョンでそのままバージョンアップ可能。
Circular References Prohibited by Default
Bean間の循環参照がデフォルトで禁止となったようです。
循環参照があると起動がBeanCurrentlyInCreationExceptionで失敗するとのこと。
基本的には循環参照は解消してあげたほうがいいと思いますが、
以下のようにpropertyで許容することができるようです。
spring.main.allow-circular-references = truespring:
main:
allow-circular-references: truePathPattern Based Path Matching Strategy for Spring MVC
デフォルトのパスの判定クラスが、AntPathMatcherからPathPatternParserに変わります。
基本的には互換性があるのですが、以下2つが差分とのこと。
{*string}という構文が追加され、0個以上のパスセグメントを@PathVariableで取得できる**を用いたマッチングがパターンの最後でのみ許容される(/**/hogeとかがNG)
参考:
それぞれもう少し深掘りしてみます
1. {*string}
以下のようなControllerを定義し、アプリケーションを起動します。
@RestController
public class SampleController {
@GetMapping("/{*path}")
public String message(@PathVariable String path) {
return path;
}
}curlで叩いてみると確かに複数パスが取得できています。
$ curl http://localhost:8080/hoge
/hoge
$ curl http://localhost:8080/hoge/fuga
/hoge/fuga2. **
以下のようなControllerを定義し、アプリケーションを起動します。
@RestController
public class SampleController {
@GetMapping("/**/hello")
public String hello() {
return "hello";
}
}起動に失敗しました。。。
(使えなくなった構文を使っているので当然ですね)
***************************
APPLICATION FAILED TO START
***************************
Description:
Invalid mapping pattern detected: /**/hello
^
No more pattern data allowed after {*...} or ** pattern element
Action:
Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'.AntPathMatcherを使いたい場合は?
既存の動きを変えたくないときですね。
起動失敗時のログにも出ていますが、以下のpropertyを設定すればOKです。
spring.mvc.pathmatch.matching-strategy=ant_path_matcherspring:
mvc:
pathmatch:
matching-strategy: ant_path_matcherActuator Env InfoContributor Disabled by Default
Actuatorのinfoエンドポイントがデフォルトで無効になるとのこと
infoを使っている場合はpropertyで有効にしてあげる必要があります
management.info.env.enabled=truemanagement:
info:
env:
enabled: trueOracle Database Driver Dependency Management
Oracleデータベースのドライバの依存関係が整理されています。
com.oracle.ojdbcグループのドライバを使用している場合はcom.oracle.database.jdbcグループを使用するよう変更する必要あり!
依存関係の整理のみのため、明示的にversionを指定してあげればcom.oracle.ojdbcを使っていてもOK!
Redis Connection Pooling
commons-pool2がクラスパスに存在する場合、デフォルトでプーリングが有効になるとのこと。
じゃあ、例えばspring-boot-starter-data-redis:2.6.0を利用した時にcommons-pool2も依存に含まれるかと言うとNoだったので、明示的に依存に追加していない限りはプーリングの影響はなさそう
無効にする場合はjedisなのかlettuceなのかに応じて以下のpropertyを設定すればOK
spring.redis.jedis.pool.enabled=false
spring.redis.lettuce.pool.enabled=falsespring:
redis:
jedis:
pool:
enabled: false
lettuce:
pool:
enabled: falseUsing WebTestClient for Testing Spring MVC
Spring MVCのテストをWebTestClientを使って記述できるようになりました!
WebTestClientの方が検証の実装が書きやすい印象なので個人的には嬉しい。
利用にはWebClientが存在する必要があるので、webのみの利用であればテストの依存でwebfluxを追加してあげる
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework:spring-webflux:5.3.4'
testImplementation 'io.projectreactor.netty:reactor-netty:1.0.4'
}テストの実装コードはこちら
@AutoConfigureMockMvc
@SpringBootTest
class SpringWebControllerTest {
@Autowired
WebTestClient webTestClient;
@Test
void test() {
webTestClient.get()
.uri("/hello")
.exchange()
.expectStatus().isOk();
}
}

