简体   繁体   English

通过Jersey发送请求时javax.ws.rs.ProcessingException

[英]javax.ws.rs.ProcessingException while sending request through Jersey

I am trying to send a JSON request to a secure REST webservice using a keystore file and I am using Jersey API. 我正在尝试使用密钥库文件向安全的REST Web服务发送JSON请求,我正在使用Jersey API。 Below is the snippet of my code 以下是我的代码片段

SslConfigurator sslConfigurator = SslConfigurator.newInstance().trustStoreFile("C:\\Users\\******\\test.keystore").trustStorePassword("password");
SSLContext sslContext = sslConfigurator.createSSLContext();
Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();
WebTarget target = client.target("https://hostname:portnumber").path("resourse/methodname/v1");

Form form = new Form();
form.param("key1", "value1");
form.param("key2", "value2");

Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
System.out.println(response);

But I am getting the following exception on the last but one line. 但是我在最后一行上得到以下异常。

Exception in thread "main" javax.ws.rs.ProcessingException: Already connected
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:264)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at TestMain.main(TestMain.java:29)
Caused by: java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3014)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:316)
at org.glassfish.jersey.client.internal.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:421)
at org.glassfish.jersey.client.internal.HttpUrlConnector.access$100(HttpUrlConnector.java:96)
at org.glassfish.jersey.client.internal.HttpUrlConnector$4.getOutputStream(HttpUrlConnector.java:384)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:262)
at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:816)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:545)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
... 10 more

It is actually a bug in jersey that the error message in the exception is basically wrong. 这实际上是泽西岛的一个错误,异常中的错误消息基本上是错误的。 Please see SSLHandshakeException masked by useless IllegalStateException: Already connected 请参阅由无用的IllegalStateException屏蔽的SSLHandshakeException:已经连接

So basically, it means that there is a problem in SSL handshaking between the server and client. 基本上,这意味着服务器和客户端之间的SSL握手存在问题。

One of the causes of the exception is related to this: Jersey API Doc - 5.9. 异常的原因之一与此相关: Jersey API Doc - 5.9。 Securing a Client 保护客户

... ClientBuilder also offers a method for defining a custom HostnameVerifier implementation. ... ClientBuilder还提供了一种定义自定义HostnameVerifier实现的方法。 HostnameVerifier implementations are invoked when default host URL verification fails. 默认主机URL验证失败时,将调用HostnameVerifier实现。

Important 重要

A behaviour of HostnameVerifier is dependent on an http client implementation. HostnameVerifier的行为取决于http客户端实现。 HttpUrlConnector and ApacheConnector work properly, that means that after the unsuccessful URL verification HostnameVerifier is called and by means of it is possible to revalidate URL using a custom implementation of HostnameVerifier and go on in a handskahe processing. HttpUrlConnector和ApacheConnector正常工作,这意味着在调用不成功的URL验证HostnameVerifier之后,通过它可以使用HostnameVerifier的自定义实现重新验证URL,并继续进行手动处理。 JettyConnector and GrizzlyConnector provide only host URL verification and throw a CertificateException without any possibility to use custom HostnameVerifier. JettyConnector和GrizzlyConnector仅提供主机URL验证并抛出CertificateException,而无法使用自定义HostnameVerifier。 Moreover, in case of JettyConnector there is a property JettyClientProperties.ENABLE_SSL_HOSTNAME_VERIFICATION to disable an entire host URL verification mechanism in a handshake. 此外,在JettyConnector的情况下,有一个属性JettyClientProperties.ENABLE_SSL_HOSTNAME_VERIFICATION来禁用握手中的整个主机URL验证机制。

If you are using Grizzly, you can't turn it off, but there is a workaround. 如果您正在使用Grizzly,则无法将其关闭,但有一种解决方法。 It is to set a customized HostnameVerifier that the verify() always returns true in the client configuration: 要设置一个自定义的HostnameVerifier,验证()始终在客户端配置中返回true:

        Client c = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier(new HostnameVerifier(){
              @Override
              public boolean verify(String paramString, SSLSession paramSSLSession) {
               return true;
             }
        }).build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 球衣javax.ws.rs.ProcessingException:已连接 - Jersey javax.ws.rs.ProcessingException: Already connected REST JAX-RS javax.ws.rs.ProcessingException: - REST JAX-RS javax.ws.rs.ProcessingException: javax.ws.rs.ProcessingException:RESTEASY004655:无法调用请求 - javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request java.lang.ClassNotFoundException:javax.ws.rs.ProcessingException - java.lang.ClassNotFoundException: javax.ws.rs.ProcessingException 在 Springboot 中重写 Docusign 方法引发了 javax.ws.rs.ProcessingException - Rewrite Docusign methods in Springboot raised javax.ws.rs.ProcessingException 泽西岛客户端抛出javax.ws.rs.ProcessingException:java.net.ConnectException:无法连接到/127.0.0.1(端口8080) - Jersey client throws javax.ws.rs.ProcessingException: java.net.ConnectException : failed to connect to /127.0.0.1 (port 8080) javax.ws.rs.ProcessingException:javax.net.ssl.SSLHandshakeException:收到致命警报:bad_certificate - javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate Java REST 客户端 - javax.ws.rs.ProcessingException:javax/xml/bind/annotation/XmlTransient - Java REST Client - javax.ws.rs.ProcessingException: javax/xml/bind/annotation/XmlTransient javax.ws.rs.ProcessingException:找不到内容类型application / json的writer - javax.ws.rs.ProcessingException: could not find writer for content-type application/json javax.ws.rs.ProcessingException:无法找到内容类型application / json的MessageBodyReader并输入类 - javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json and type class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM