KyLim0211 Ответов: 1

Java catch исключение, когда url-адрес недоступен


Я запускаю свой java-код с помощью Tomcat,в настоящее время он показывает кучу ошибок всякий раз, когда целевая конечная точка недоступна/недоступна/не может получить никакого ответа. Я попытался добавить исключение catch в свой код, но оно, похоже, не работает.

reportUrl = "http://Unavailable";
                HttpEntity<DrEndpointRequest> entity = new HttpEntity<>(der, headers);
                try {
                    ResponseEntity<String> response = restTemplate.exchange(reportUrl, HttpMethod.POST, entity, String.class);
                    System.out.println("Response-- " + response.getBody() + " " + response.getStatusCode());
                    if (response.getStatusCode() == HttpStatus.OK) {
                        trans.setDnStatus("Sent");
                    } else {
                        trans.setDnStatus("Failed");
                    }
                } catch (HttpStatusCodeException ex) {
                    System.out.println(ex.getStatusText() + "   " + ex.getResponseBodyAsString());
                    trans.setDnStatus("Failed");
                }



ниже приведена ошибка, показанная в tomcat :

2019-04-30 02:33:55.016 ERROR 4432 --- [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://Unavailable": Unavailable; nested exception is java.net.UnknownHostException: Unavailable at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at com.dr.scheduler.ScheduledTask.scheduleTaskWithFixedRate(ScheduledTask.java:97) ~[classes/:1.0] at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_201] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_201] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_201] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_201] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201] Caused by: java.net.UnknownHostException: Unavailable at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_201] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_201] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_201] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_201] at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_201] at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.(HttpClient.java:242) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_201] at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) ~[na:1.8.0_201] at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984) ~[na:1.8.0_201] at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:734) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 15 common frames omitted


Что я уже пробовал:

я добавил:
catch (HttpStatusCodeException ex) {
                    System.out.println(ex.getStatusText() + "   " + ex.getResponseBodyAsString());
                    trans.setDnStatus("Failed");
                }

но не работает

Richard MacCutchan

Он может поймать только исключение, которое вы ему скажете искать. В вашем случае система выбросила другой.

1 Ответов

Рейтинг:
2

phil.o

Попробовать перехватить ResourceAccessException или UnknownHostException вместо.