我正在使用此测试代码调用REST服务:
public class TestRESTServices {
private static final String BASE_URL = "http://localhost/ma.ge.persistence-1.0/rest/reference";
private static URI uri = UriBuilder.fromUri(BASE_URL).port(8080).build();
private static Client client = ClientBuilder.newClient();
@Test
public void createAndDeleteAReference() {
Reference r = ReferenceFactory.createReference("Maz",
"dummy", 1.7);
Response response = client.target(uri).request().post(Entity.entity(r, MediaType.APPLICATION_JSON));
assertEquals(Response.Status.CREATED, response.getStatusInfo());
URI referenceURI = response.getLocation();
// Get the posted reference
response = client.target(referenceURI).request().get();
Reference retreivedRef = response.readEntity(Reference.class);
assertEquals(Response.Status.OK, response.getStatusInfo());
assertEquals(retreivedRef.getName(), r.getName());
}
但是我收到以下错误:
javax.ws.rs.ProcessingException:无法调用请求 在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287) 在org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407) 在org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:159) at ma.gesto.persistence.TestRESTServices.createAndDeleteAReference(TestRESTServices.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 引起:java.lang.IllegalStateException:无效使用BasicClientConnManager:仍然分配连接。 确保在分配另一个连接之前释放连接。 at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:162) at org.apache.http.impl.conn.BasicClientConnectionManager $ 1.getConnection(BasicClientConnectionManager.java:139) 在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:456) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283) ......还有26个
答案 0 :(得分:6)
Client
接口表示外部资源。因此,在静态变量中创建和存储一个是一个严重的错误。这是错的:
private static Client client = ClientBuilder.newClient();
这适用于单元测试:
private Client client;
@Before
public void setUp() {
this.client = ClientBuilder.newClient();
}
@After
public void tearDown() {
this.client.close();
}
在普通代码中,您希望在Client
或try-with-resources
中包含try..finally
用法:
Client client = ClientBuilder.newClient();
try {
// use the client to make requests
} finally {
client.close();
}
理想情况下,有一种方法可以管理Client
个实例池以供重用。
答案 1 :(得分:0)
您还需要关闭响应以释放连接。 请参阅此处的说明: Resteasy Client keeping connection allocated after a method throws an Exception