简体   繁体   English

如何在 CXF 客户端中使用 PATCH 方法

[英]How to use PATCH method in CXF Client

I was trying to call a API with PATCH method using CXF (Version 3.1.3) client.我试图使用 CXF(版本 3.1.3)客户端通过 PATCH 方法调用 API。

Tried following the steps specified in below threads but not able resolve.尝试按照以下线程中指定的步骤操作,但无法解决。 Only getting URLConnectionHttpConduit instead of AsyncHttpConduit只获取 URLConnectionHttpConduit 而不是 AsyncHttpConduit

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

How to use PATCH method in CXF 如何在 CXF 中使用 PATCH 方法

Verifying CXF HttpAsyncClient use of use.async.http.conduit contextual property 验证 CXF HttpAsyncClient 对 use.async.http.conduit 上下文属性的使用

Here is the code snippet:这是代码片段:

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

I even tried using X-HTTP-Method-Override=PATCH header with a POST request,我什至尝试在 POST 请求中使用 X-HTTP-Method-Override=PATCH 标头,

Other side service is implemented using RestEasy and look's like not honoring X-HTTP-Method-Override header.其他边服务是使用 RestEasy 实现的,看起来像是不尊重 X-HTTP-Method-Override 标头。

Can you please help me finding the issue.你能帮我找到问题吗。

When we got similar problem, we have used CloseableHttpAsyncClient and it works fine.当我们遇到类似问题时,我们使用了CloseableHttpAsyncClient并且它工作正常。 Below is the sample code for your reference:以下是供您参考的示例代码:

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;

You can refer to the link for any more details.您可以参考链接了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM