[JAVA] Gère diverses erreurs (exceptions) qui se produisent lors de la communication HTTP avec RestTemplate

Aperçu

Arborescence des relations d'héritage pour la classe d'exception RestClientException

En regardant la documentation de Spring Framework, RestTemplate indique qu'il lance org.springframework.web.client.RestClientException. Si vous interceptez l'exception et vérifiez le contenu, vous pouvez voir que la sous-classe de RestClientException est réellement levée.

--RestClientException (la classe sur laquelle est basée l'exception levée par RestTemplate) --RestClientResponseException (la classe sur laquelle l'exception contient des données de réponse HTTP) --HttpStatusCodeException (classe abstraite d'exception basée sur le code d'état) --HttpClientErrorException (lors de la réception de la série HTTP 4xx) --Chaque classe d'exception (pluriel) du code d'état de la série 4xx --HttpServerErrorException (lors de la réception de la série HTTP 5xx) --Chaque classe d'exception (pluriel) du code d'état de la série 5xx --UnknownHttpStatusCodeException (lorsque le code d'état est inconnu) --ResourceAccessException (lorsqu'une erreur d'E / S se produit)

Liste des exceptions survenues dans l'exemple de programme

Pour HTTP 404 introuvable

--Exception survenue: org.springframework.web.client.HttpClientErrorException $ NotFound --HttpClientErrorException.NotFound est une sous-classe de org.springframework.web.client.HttpClientErrorException

Pour l'erreur de serveur interne HTTP 500

--Exception survenue: org.springframework.web.client.HttpServerErrorException $ InternalServerError --HttpServerErrorException.InternalServerError est une sous-classe de org.springframework.web.client.HttpServerErrorException

Pour un code d'état inconnu

--Exception survenue: org.springframework.web.client.UnknownHttpStatusCodeException

Sinon une chaîne d'URL

--Exception: java.lang.IllegalArgumentException

S'il ne peut pas être analysé en tant que chaîne d'URL

--Exception survenue: java.net.URISyntaxException

Si le nom du schéma de l'URL est inconnu

--Exception survenue: org.springframework.web.client.ResourceAccessException --Causing exception: java.net.MalformedURLException

Lorsque l'adresse IP ne peut pas être soustraite du nom d'hôte

--Exception survenue: org.springframework.web.client.ResourceAccessException --Causing exception: java.net.UnknownHostException

Lors de l'accès à un port sur lequel le serveur Web n'est pas démarré (connexion refusée)

--Exception survenue: org.springframework.web.client.ResourceAccessException --Cause d'exception: java.net.ConnectException

Pour les certificats de serveur SSL non approuvés (certificat auto-signé, certificat Oleore)

--Exception survenue: org.springframework.web.client.ResourceAccessException --Cause d'exception: javax.net.ssl.SSLHandshakeException

En cas d'expiration du délai de connexion (par exemple, adresse IP où la machine n'existe pas)

--Exception survenue: org.springframework.web.client.ResourceAccessException --Cause d'exception: java.net.SocketTimeoutException

En cas de délai de lecture

--Exception survenue: org.springframework.web.client.ResourceAccessException --Cause d'exception: java.net.SocketTimeoutException

Environnement de contrôle de fonctionnement

Code source de l'exemple de programme

Liste des codes sources

├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── example
                    └── MyHttpClient.java

Fichier de construction Maven pom.xml

Cette fois, créez l'exemple de programme avec Maven. La classe RestTemplate est incluse dans le Spring Web du Spring Framework, ajoutez donc spring-web aux dépendances.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>my-http-client</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <!-- http://maven.apache.org/plugins/maven-assembly-plugin/ -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.example.MyHttpClient</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.8.RELEASE</version>
    </dependency>
  </dependencies>

</project>

MyHttpClient.java

Une classe pour vérifier diverses exceptions qui se produisent pendant la communication HTTP.

package com.example;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

public class MyHttpClient {

  public static void main(String[] args) {

    RestTemplate restTemplate = new RestTemplate();

    //Définir la valeur du délai d'expiration
    if(restTemplate.getRequestFactory() instanceof SimpleClientHttpRequestFactory) {
      SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory)restTemplate.getRequestFactory();
      rf.setConnectTimeout(1000); //Définir le délai d'expiration de la connexion à 1000 millisecondes
      rf.setReadTimeout(1000); //Définir le délai de lecture sur 1000 millisecondes
    }

    //Renvoie le nom de classe de ClientHttpRequestFactory utilisé en interne par RestTemplate
    System.out.println("ClientHttpRequestFactory: " + restTemplate.getRequestFactory().getClass().getName());

    //Essayez différents modèles
    get("HTTP 200 OK", restTemplate, "http://localhost:8000/");
    get("HTTP 301 Moved Permanently (Les redirections doivent être gérées automatiquement)", restTemplate, "http://localhost:8000/?status=301&headers=%7B%22location%22%3A%22http%3A%2F%2Flocalhost%3A8000%2F%22%7D");
    get("HTTP 404 Not Found", restTemplate, "http://localhost:8000/?status=404");
    get("HTTP 500 Internal Server Error", restTemplate, "http://localhost:8000/?status=500");
    get("Code d'état inconnu", restTemplate, "http://localhost:8000/?status=599");
    get("Pas une chaîne d'URL", restTemplate, "foo");
    get("Ne peut pas être analysé en tant que chaîne d'URL", restTemplate, "http://local host:8000/");
    get("Nom du schéma d'URL inconnu", restTemplate, "foo://localhost:8000/");
    get("L'adresse IP ne peut pas être soustraite du nom d'hôte", restTemplate, "http://localhostlocalhost:8000/");
    get("Port sur lequel le serveur Web n'est pas démarré", restTemplate, "http://localhost:1234/");
    get("Certificat de serveur SSL non approuvé(Certificat auto-signé,Certificat Oreore)", restTemplate, "https://localhost:8443/");
    get("Délai de connection dépassé(Connection Timeout)Adresse IP où la machine n'existe pas", restTemplate, "http://127.0.0.2/");
    get("Délai de lecture(Read Timeout)", restTemplate, "http://localhost:8000/?wait=10");
  }

  /**
   *Accédez à l'URL spécifiée et affichez les informations.
   *
   * @titre du paramètre(Explication de quel type de modèle)
   * @param restTemplate Objet RestTemplate utilisé pour la communication HTTP
   * @URL du paramètre pour accéder
   */
  private static void get(String title, RestTemplate restTemplate, String url) {
    try {
      System.out.println("************************************************************");
      System.out.println(title);
      System.out.println("URL: " + url);
      URI uri = new URI(url);
      RequestEntity req = RequestEntity.get(uri).build();
      ResponseEntity<String> res = restTemplate.exchange(req, String.class);
      HttpStatus status = res.getStatusCode();
      HttpHeaders headers = res.getHeaders();
      String body = res.getBody();
      System.out.println("Status: " + status);
    } catch (RestClientResponseException e) {
      System.out.println("Class: " + e.getClass().getName());
      int statusCode = e.getRawStatusCode();
      String statusText = e.getStatusText();
      System.out.println("StatusCode: " + statusCode);
      System.out.println("StatusText: " + statusText);
      e.printStackTrace();
    } catch (ResourceAccessException e) {
      System.out.println("Class: " + e.getClass().getName());
      e.printStackTrace();
    } catch (RestClientException e) {
      System.out.println("Class: " + e.getClass().getName());
      e.printStackTrace();
    } catch (URISyntaxException e) {
      System.out.println("Class: " + e.getClass().getName());
      e.printStackTrace();
    } catch (Exception e) {
      System.out.println("Class: " + e.getClass().getName());
      e.printStackTrace();
    }
  }
}

Construction du code source

Générez un fichier JAR avec la commande mvn package.

$ mvn package

Résultat d'exécution

Exécutez l'exemple de programme pour voir quelles exceptions se produisent dans chaque modèle.

$ java -jar target/my-http-client-1.0-SNAPSHOT-jar-with-dependencies.jar 
ClientHttpRequestFactory: org.springframework.http.client.SimpleClientHttpRequestFactory
************************************************************
HTTP 200 OK
URL: http://localhost:8000/
Status: 200 OK
************************************************************
HTTP 301 Moved Permanently (Les redirections doivent être gérées automatiquement)
URL: http://localhost:8000/?status=301&headers=%7B%22location%22%3A%22http%3A%2F%2Flocalhost%3A8000%2F%22%7D
Status: 200 OK
************************************************************
HTTP 404 Not Found
URL: http://localhost:8000/?status=404
Class: org.springframework.web.client.HttpClientErrorException$NotFound
StatusCode: 404
StatusText: Not Found
org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
	at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:35)
************************************************************
HTTP 500 Internal Server Error
URL: http://localhost:8000/?status=500
Class: org.springframework.web.client.HttpServerErrorException$InternalServerError
StatusCode: 500
StatusText: Internal Server Error
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error
	at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:79)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:36)
************************************************************
Code d'état inconnu
URL: http://localhost:8000/?status=599
Class: org.springframework.web.client.UnknownHttpStatusCodeException
StatusCode: 599
StatusText: null
org.springframework.web.client.UnknownHttpStatusCodeException: Unknown status code [599] null
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:100)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:37)
************************************************************
Pas une chaîne d'URL
URL: foo
Class: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: URI is not absolute
	at java.base/java.net.URL.fromURI(URL.java:674)
	at java.base/java.net.URI.toURL(URI.java:1116)
	at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
	at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:87)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:731)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:38)
************************************************************
Ne peut pas être analysé en tant que chaîne d'URL
URL: http://local host:8000/
Class: java.net.URISyntaxException
java.net.URISyntaxException: Illegal character in authority at index 7: http://local host:8000/
	at java.base/java.net.URI$Parser.fail(URI.java:2915)
	at java.base/java.net.URI$Parser.parseAuthority(URI.java:3249)
	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3160)
	at java.base/java.net.URI$Parser.parse(URI.java:3116)
	at java.base/java.net.URI.<init>(URI.java:600)
	at com.example.MyHttpClient.get(MyHttpClient.java:60)
	at com.example.MyHttpClient.main(MyHttpClient.java:39)
************************************************************
Nom du schéma d'URL inconnu
URL: foo://localhost:8000/
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "foo://localhost:8000/": unknown protocol: foo; nested exception is java.net.MalformedURLException: unknown protocol: foo
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:40)
Caused by: java.net.MalformedURLException: unknown protocol: foo
	at java.base/java.net.URL.<init>(URL.java:634)
	at java.base/java.net.URL.fromURI(URL.java:701)
	at java.base/java.net.URI.toURL(URI.java:1116)
	at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
	at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:87)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:731)
	... 3 more
************************************************************
L'adresse IP ne peut pas être soustraite du nom d'hôte
URL: http://localhostlocalhost:8000/
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhostlocalhost:8000/": localhostlocalhost; nested exception is java.net.UnknownHostException: localhostlocalhost
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:41)
Caused by: java.net.UnknownHostException: localhostlocalhost
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
	at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009)
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
	... 3 more
************************************************************
Port sur lequel le serveur Web n'est pas démarré
URL: http://localhost:1234/
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:1234/": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:42)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
	at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009)
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
	... 3 more
************************************************************
Certificat de serveur SSL non approuvé(Certificat auto-signé,Certificat Oreore)
URL: https://localhost:8443/
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8443/": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:43)
Caused by: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:128)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
	at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:642)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.onCertificate(CertificateMessage.java:461)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.consume(CertificateMessage.java:361)
	at java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:392)
	at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:444)
	at java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:421)
	at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:178)
	at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
	at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1152)
	at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1063)
	at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402)
	at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)
	at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
	at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:163)
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
	... 3 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
	at java.base/sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:290)
	at java.base/sun.security.validator.Validator.validate(Validator.java:264)
	at java.base/sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:321)
	at java.base/sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:221)
	at java.base/sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:129)
	at java.base/sun.security.ssl.CertificateMessage$T12CertificateConsumer.checkServerCerts(CertificateMessage.java:626)
	... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at java.base/sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
	at java.base/sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
	at java.base/java.security.cert.CertPathBuilder.build(CertPathBuilder.java:297)
	at java.base/sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
	... 26 more
************************************************************
Délai de connection dépassé(Connection Timeout)Adresse IP où la machine n'existe pas
URL: http://127.0.0.2/
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://127.0.0.2/": connect timed out; nested exception is java.net.SocketTimeoutException: connect timed out
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:44)
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
	at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
	at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341)
	at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009)
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
	... 3 more
************************************************************
Délai de lecture(Read Timeout)
URL: http://localhost:8000/?wait=10
Class: org.springframework.web.client.ResourceAccessException
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8000/": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:637)
	at com.example.MyHttpClient.get(MyHttpClient.java:62)
	at com.example.MyHttpClient.main(MyHttpClient.java:45)
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.base/java.net.SocketInputStream.socketRead0(Native Method)
	at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
	at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
	at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
	at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:252)
	at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:292)
	at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:351)
	at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:746)
	at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:689)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1604)
	at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
	at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:527)
	at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:82)
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
	at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
	... 3 more

Matériel de référence

Recommended Posts

Gère diverses erreurs (exceptions) qui se produisent lors de la communication HTTP avec RestTemplate
Prise en compte et contre-mesures selon lesquelles les erreurs de communication SSL sont plus susceptibles de se produire à partir de Java 11