POST API с использованием Spring WebClient через HTTP2

Я пишу JUNIT для push-уведомлений с использованием токенов аутентификации. Теперь API Apple push основан на HTTP2, и я хочу использовать для этой цели spring WebClient.

private static WebClient webClient = WebClient
            .builder()
            .baseUrl("https://api.development.push.apple.com/3/device")
                .defaultHeader("apns-priority", "10")
                .defaultHeader("apns-expiration", "0")
                .build();

    class ApnsResponse {
        String reason;
        String timestamp;
    }

    @Test
    void test1() {
        Hooks.onOperatorDebug();
        String message = "{\"aps\":{\"alert\":\"hello\"}}";
        try {
        webClient
            .post()
                .uri("/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .header("authorization", "bearer "+authToken)
                .header("apns-topic", "com.demo.12316")
                .bodyValue(message)
                .retrieve()
                .bodyToMono(ApnsResponse.class)
                .block();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

Когда я запускаю тестовый пример, я получаю:

21:37:26.028 [reactor-http-nio-1] DEBUG io.netty.handler.ssl.SslHandler - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] HANDSHAKEN: protocol:TLSv1.2 cipher suite:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
21:37:26.030 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}, [connected])
21:37:26.062 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [configured])
21:37:26.070 [reactor-http-nio-1] DEBUG reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] Handler is being applied: {uri=https://api.development.push.apple.com/3/device/6d268d2d494f512dfa1212aa612c1822f44df4375e9116b5a52109f3a21d1, method=POST}
21:37:26.075 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_prepared])
21:37:26.111 [reactor-http-nio-1] DEBUG org.springframework.core.codec.CharSequenceEncoder - [6304101a] Writing "{"aps":{"alert":"hello"}}"
21:37:26.183 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 - R:api.development.push.apple.com/17.188.165.219:443]}}, [request_sent])
21:37:26.559 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] Channel closed, now 0 active connections and 0 inactive connections
21:37:26.569 [reactor-http-nio-1] DEBUG reactor.netty.resources.PooledConnectionProvider - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] onStateChange(POST{uri=/3/device/6d268d12394f512dfa1212aa612c1822f44df4312116b5a52109f3a21d1, connection=PooledConnection{channel=[id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443]}}, [response_incomplete])
21:37:26.592 [reactor-http-nio-1] WARN reactor.netty.http.client.HttpClientConnect - [id: 0xddac3403, L:/192.168.11.13:63108 ! R:api.development.push.apple.com/17.188.165.219:443] The connection observed an error
reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
reactor.core.Exceptions$ReactiveException: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

Мне удалось использовать WebClient для HTTP-вызовов, но в этом случае он просто не работал. Я использую JAVA 1.8. Я даже попытался добавить ALPN в путь к загрузочному классу, но все равно получаю ту же ошибку.

Поддерживает ли WebClient HTTP2? Или мне нужно начать изучать OkHTTP2.

Тем временем я читаю о clientConnector, который используется при создании WebClient.

0 ответов

Другие вопросы по тегам