当出现连接错误时,我正在尝试添加控件。我的目标是通过捕获它来阻止程序抛出异常。
try {
Client client = ClientBuilder.newBuilder()
.register(JacksonFeature.class)
.build();
client.register(new HttpBasicAuthFilter("login", "password"));
WebTarget webTarget = client.target("http://path");
Response response = webTarget.request().post(Entity.json(bean));
System.out.println(response.getStatus());
} catch (Exception e) {
// I do nothing here
}
但它在控制台中打印:
org.glassfish.jersey.client.ClientRequest writeEntity Grave:提交请求输出流时出错。java.net.ConnectException:连接被拒绝:在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java: 339) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 在 java. net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 在 sun.net.sun.net.NetworkClient.doConnect(NetworkClient.java:175) 在 java.net.Socket.connect(Socket.java:579) 在 sun.net。
如果我添加捕获部分:e.printStackTrace();
控制台还打印:
原因:java.net.ConnectException:连接被拒绝:在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl) 处连接 java.net.DualStackPlainSocketImpl.connect0(Native Method) .java:339) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 在 java.net.Socket.connect(Socket.java:579) 在 sun.net.NetworkClient.doConnect(NetworkClient.java:175) 在 sun.net。 www.http.HttpClient.openServer(HttpClient.java:432) 在 sun.net.www.http.HttpClient。openServer(HttpClient.java:527) 在 sun.net.www.http.HttpClient.(HttpClient.java:211) 在 sun.net.www.http.HttpClient.New(HttpClient.java:308) 在 sun.net。 www.http.HttpClient.New(HttpClient.java:326) 在 sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996) 在 sun.net.www.protocol.http.HttpURLConnection.plainConnect( HttpURLConnection.java:932) 在 sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850) 在 sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091) 在 org .glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:287) 在 org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198) 在 org.glassfish.jersey.message.internal。在 org.codehaus.jackson.impl.Utf8Generator._flushBuffer(Utf8Generator.java:1754) 的 org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:226) 的 CommittingOutputStream.commitStream(CommittingOutputStream.java:192)在 org.codehaus.jackson.impl.Utf8Generator.flush(Utf8Generator.java:1088) 在 org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1615) 在 org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo (JacksonJsonProvider.java:559) 在 org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194) 在 org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139 ) 在 org.glassfish.jersey.message.internal.MessageBodyFactory。writeTo(MessageBodyFactory.java:1005) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430) at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290) at org.glassfish .jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203) ... 还有 78 个
抱歉,如果我打印了太多堆栈跟踪...
我想知道如果我不打印堆栈跟踪,我怎样才能让异常消失?谢谢 :)