我创建了一个基于RESTET的Web服务,该服务使用Post/json。
问题是,在本地机器上测试时,
它运行良好。但是,当它部署在服务器上时,它
比对我的本地开发进行测试的大约10秒钟
机器即使没有其他进程使用
,也可以获取响应服务器的资源。
我通过使用apache http客户端而不是
解决了问题来自雷斯特的客户率。
我在远程Linux服务器上测试了ClientResource和Apache HTTP的同一基于雷斯特的Web服务。前者花了大约10秒后者不到1秒。我不知道为什么。
public class Test
{
public static void main(String [] args)
{
HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead
try {
HttpPost request = new HttpPost("http://hostname:port/testService");
StringEntity params =new StringEntity("{"key" : "value"}");
request.addHeader("content-type", "application/json");
request.addHeader("Accept", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
String json_string = EntityUtils.toString(response.getEntity());
System.out.println(json_string);
}catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown(); //Deprecated
}
}
}