如何在CXF Client中使用PATCH方法
How to use PATCH method in CXF Client
我试图使用 CXF(版本 3.1.3)客户端通过 PATCH 方法调用 API。
已尝试按照以下线程中指定的步骤进行操作,但无法解决。
仅获取 URLConnectionHttpConduit 而不是 AsyncHttpConduit
http://cxf.apache.org/docs/asynchronous-client-http-transport.html
这是代码片段:
Bus bus = BusFactory.getDefaultBus();
// insist on the async connector to use PATCH.
bus.setProperty(AsyncHTTPConduit.USE_ASYNC,
AsyncHTTPConduitFactory.UseAsyncPolicy.ALWAYS);
WebClient webClient = WebClient.create(request.getRestURL());
WebClient.getConfig(webClient).getBus().setProperty
(AsyncHTTPConduit.USE_ASYNC, AsyncHTTPConduitFactory.UseAsyncPolicy.ALWAYS);
WebClient.getConfig(webClient).getRequestContext()
.put(AsyncHTTPConduit.USE_ASYNC, AsyncHTTPConduitFactory.
UseAsyncPolicy.ALWAYS);
HTTPConduit conduit = (HTTPConduit)WebClient.getConfig(webClient)
.getConduit();
System.out.println(conduit.getClass().getName());
Response response = webClient.invoke(request.getMethod(), null);
System.out.println("service response = "+ response);
我什至尝试使用 X-HTTP-Method-Override=PATCH header 和 POST 请求,
其他端服务是使用 RestEasy 实现的,看起来不符合 X-HTTP-Method-Override header。
你能帮我找到问题吗?
当我们遇到类似问题时,我们使用了 CloseableHttpAsyncClient
并且效果很好。以下是供您参考的示例代码:
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(10).build();
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(10);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30000)
.setSocketTimeout(30000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setConnectionManager(cm)
.setConnectionManagerShared(false)
.setDefaultRequestConfig(requestConfig)
.build();
httpclient.start();
HttpPatch httpReq = new HttpPatch(url);
StringEntity entity = new StringEntity(json);
httpReq.setEntity(entity);
Future<HttpResponse> future = httpclient.execute(httpReq, context, null);
HttpResponse httpResponse = future.get();
HttpEntity responseEntity = httpResponse.getEntity();
String responseText = responseEntity != null ? EntityUtils.toString(responseEntity) : null;
您可以参考link了解更多详情。
我试图使用 CXF(版本 3.1.3)客户端通过 PATCH 方法调用 API。
已尝试按照以下线程中指定的步骤进行操作,但无法解决。 仅获取 URLConnectionHttpConduit 而不是 AsyncHttpConduit
http://cxf.apache.org/docs/asynchronous-client-http-transport.html
这是代码片段:
Bus bus = BusFactory.getDefaultBus();
// insist on the async connector to use PATCH.
bus.setProperty(AsyncHTTPConduit.USE_ASYNC,
AsyncHTTPConduitFactory.UseAsyncPolicy.ALWAYS);
WebClient webClient = WebClient.create(request.getRestURL());
WebClient.getConfig(webClient).getBus().setProperty
(AsyncHTTPConduit.USE_ASYNC, AsyncHTTPConduitFactory.UseAsyncPolicy.ALWAYS);
WebClient.getConfig(webClient).getRequestContext()
.put(AsyncHTTPConduit.USE_ASYNC, AsyncHTTPConduitFactory.
UseAsyncPolicy.ALWAYS);
HTTPConduit conduit = (HTTPConduit)WebClient.getConfig(webClient)
.getConduit();
System.out.println(conduit.getClass().getName());
Response response = webClient.invoke(request.getMethod(), null);
System.out.println("service response = "+ response);
我什至尝试使用 X-HTTP-Method-Override=PATCH header 和 POST 请求,
其他端服务是使用 RestEasy 实现的,看起来不符合 X-HTTP-Method-Override header。
你能帮我找到问题吗?
当我们遇到类似问题时,我们使用了 CloseableHttpAsyncClient
并且效果很好。以下是供您参考的示例代码:
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(10).build();
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(10);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30000)
.setSocketTimeout(30000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setConnectionManager(cm)
.setConnectionManagerShared(false)
.setDefaultRequestConfig(requestConfig)
.build();
httpclient.start();
HttpPatch httpReq = new HttpPatch(url);
StringEntity entity = new StringEntity(json);
httpReq.setEntity(entity);
Future<HttpResponse> future = httpclient.execute(httpReq, context, null);
HttpResponse httpResponse = future.get();
HttpEntity responseEntity = httpResponse.getEntity();
String responseText = responseEntity != null ? EntityUtils.toString(responseEntity) : null;
您可以参考link了解更多详情。