简体   繁体   中英

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.

Tried following the steps specified in below threads but not able resolve. Only getting URLConnectionHttpConduit instead of 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

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,

Other side service is implemented using RestEasy and look's like not honoring X-HTTP-Method-Override header.

Can you please help me finding the issue.

When we got similar problem, we have used CloseableHttpAsyncClient and it works fine. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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