本文介绍了当url不可用时,Java捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Tomcat运行我的java代码,目前只要目标端点无法访问/不可用/无法获得任何响应,它就会显示大量错误。我试图在我的代码中添加catch异常,但似乎无效。
reportUrl = http:// Unavailable;
HttpEntity< DrEndpointRequest> entity = new HttpEntity<>(der,headers);
尝试 {
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( 已发送);
} else {
trans.setDnStatus( 失败);
}
} catch (HttpStatusCodeException ex){
System.out.println(ex.getStatusText()+ + ex.getResponseBodyAsString());
trans.setDnStatus( 失败);
}
以下是tomcat中显示的错误:
2019-04-30 02:33:55.016 ERROR 4432 --- [scheduling-1] osssTaskUtils $ LoggingErrorHandler:计划任务中发生意外错误。
org.springframework.web.client.ResourceAccessException:POST请求中的http:// Unavailable的I / O错误:不可用;嵌套异常是java.net.UnknownHostException:在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)〜[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]中不可用org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)〜[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] 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 ]在sun.reflect.GeneratedMethodAccessor89.invoke(未知来源)〜[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]在org.springframework.scheduling.support.DelegatingErrorHandlin java.util.concurrent.Executors中的gRunnable.run(DelegatingErrorHandlingRunnable.java:54)〜[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] $ 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) java [u :. java [.u :. :1.8.0_201]引起:java.net.UnknownHostException:java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)〜[na:1.8.0_201]在java.net.PlainSocketImpl.connect(PlainSocketImpl.java)中不可用: 172)〜[na:1.8.0_201] java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)〜[na:1.8.0_201] 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] 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]在org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)〜[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] org.springframework.web.client.RestTemplate。 doExecute(RestTemplate.java:734)〜[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE] ...省略了15个常用框架
我尝试过:
i添加
catch(HttpStatusCodeException ex){
System.out.println(ex.getStatusText()++ ex.getResponseBodyAsString());
trans.setDnStatus(失败);
}
但没有工作
解决方案
I am running my java code using Tomcat,currently it shows bunch of errors whenever the target endpoint is not reachable/unavailable/cant get any response. I tried to add catch exception in my code but it seems not working.
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"); }
below is the error shown in 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
What I have tried:
i added
catch (HttpStatusCodeException ex) { System.out.println(ex.getStatusText() + " " + ex.getResponseBodyAsString()); trans.setDnStatus("Failed"); }
but not working
解决方案
这篇关于当url不可用时,Java捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!