尝试使用Jersey Client API执行POST请求的“已连接”异常

时间:2015-03-09 21:01:29

标签: java jersey client jboss-arquillian

我正在使用arquillian为Tomcat 8上部署的JAX-RS / Jersey Web服务创建集成测试。

我正在尝试执行类似的POST请求:

E dummy = dummyFactory.manufacturePojo(getSubClassType());
dummy.setId(null);

Client client = ClientBuilder.newClient();
WebTarget target = client.target(BASE_URI).path("bandeira");

Response response = target.request(MediaType.APPLICATION_JSON)
            .header(HttpHeaders.AUTHORIZATION, CHAVE_TESTE)
            .header(HttpHeaders.CONTENT_TYPE, "application/json")
            .post(Entity.entity(dummy, MediaType.APPLICATION_JSON));

当我这样做时,我得到了这个例外:

Caused by: java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3000)
at org.glassfish.jersey.client.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:364)
at org.glassfish.jersey.client.HttpUrlConnector.access$100(HttpUrlConnector.java:91)
at org.glassfish.jersey.client.HttpUrlConnector$4.getOutputStream(HttpUrlConnector.java:327)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:201)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:195)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:263)
at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:816)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:546)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:331)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:243)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
... 149 more

我可以使用一些启发式,因为我还在学习arquillian和Jersey客户端API :) 谢谢

4 个答案:

答案 0 :(得分:24)

可能java.lang.IllegalStateException: Already connected仅屏蔽SSLHandshakeException。请查看问题#3000(以前称为JERSEY-2728错误)。

答案 1 :(得分:3)

这可能是由于网络连接问题。 我遇到了这个问题,因为我的VPN丢失了连接。 " Already connected"的例外情况在杰克逊序列化邮件正文期间报道。(我导入了Jersey和jackson-jaxrs-base的源代码进行调试)。 删除邮件正文后,出现了错误的新异常" Unknown hostname&#34 ;.

登录VPN后,一切正常。

我对泽西客户异常"Already connected"非常不满意。这给了我什么,只有混乱。

答案 2 :(得分:2)

问题可能在于SSL协商。尝试添加" trustall"客户初始化逻辑。

SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

} }, new java.security.SecureRandom());

Client client = ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true)
.register(MultiPartFeature.class)
.register(new EncodingFeature("gzip", GZipEncoder.class))
.build();

答案 3 :(得分:2)

调用connection.connect();

后,您可能在http连接上设置了header属性