以前はHTTP/2試すのに色々と設定が必要だったが、https://hirakida29.hatenablog.com/entry/2019/11/30/231927 で書かれているようにSpring Boot 2.5以降はプロパティを一つ設定するだけになった。これを試す。
ソースコード
build.gradle
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
以下のプロパティでHTTP/2が有効になる。
src/main/resources/application.properties
server.http2.enabled true
動作確認用に適当なRestController
を作る。
@RestController
@SpringBootApplication
public class App {
@GetMapping("/hoge")
public String hoge() {
return "hoge";
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
動作確認用の適当なclientを作る。
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class ClientSample {
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
// HttpClient client = HttpClient.newBuilder().version(Version.HTTP_2).build();
HttpRequest request = HttpRequest.newBuilder(new URI("http://localhost:8080/hoge")).build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.version());
}
}
これを実行するとHTTP_2
と表示される。
HttpClient.newHttpClient()
のようにversion未指定だとデフォルトはVersion.HTTP_2
なので、特にversion指定の必要はない。