gpt4 book ai didi

java - 如何在CXF Client中使用PATCH方法

转载 作者:行者123 更新时间:2023-11-30 08:44:22 27 4
gpt4 key购买 nike

我尝试使用 CXF(版本 3.1.3)客户端通过 PATCH 方法调用 API。

尝试按照以下线程中指定的步骤进行操作,但无法解决。只获取 URLConnectionHttpConduit 而不是 AsyncHttpConduit

http://cxf.apache.org/docs/asynchronous-client-http-transport.html

How to use PATCH method in CXF

Verifying CXF HttpAsyncClient use of use.async.http.conduit contextual property

这是代码片段:

    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);

我什至尝试在 POST 请求中使用 X-HTTP-Method-Override=PATCH header ,

其他端服务是使用 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了解更多详情。

关于java - 如何在CXF Client中使用PATCH方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808263/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com