PATCH HTTP Method : java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/TrustAllStrategy in Java 8

186 views Asked by At

I am posting JSON Payload to REST API by using PATCH method with the help of apache http client libraries

Java Version: 1.8.0_171

Following this link for PATCH method with apache http client

Code snippet in blog:

try {

        //This is just to avoid ssl hostname verification and to trust all, you can use simple Http client also
        CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();

        HttpPatch request = new HttpPatch(REST_SERVICE_URL);
        StringEntity params = new StringEntity(JSON.toJSONString(payload), ContentType.APPLICATION_JSON);
        request.setEntity(params);
        request.addHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
        request.addHeader(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
        HttpResponse response =     httpClient.execute(request);            

        String statusCode = response.getStatusLine().getStatusCode();

    } catch (Exception ex) {
        // handle exception here
    }

What I tried:

import java.util.Base64;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;


public class Connect2Client {
public static void main(String[] args) {
    String url = "https://client.domain.com/client/api/A1234";

    String Credentials = "XXXX";
    String paylod="[{ \"op\": \"add\", \"path\": \"/progressTrail\", \"value\": \"messageType update\", \"from\" : \"string\" } ]";
    try {
        String returnPayload=connect(url, Credentials, paylod);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static String connect(String url, String Credentials, String data) throws Exception {
    String resp = null;
     //This is just to avoid ssl hostname verification and to trust all, you can use simple Http client also
    CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
    HttpPatch request = new HttpPatch(url);
    StringEntity params = new StringEntity(data);
    String authStringEnc = Base64.getEncoder().encodeToString(Credentials.getBytes());
    request.setEntity(params);
    request.addHeader("Content-Type", "application/json-patch+json");
    request.addHeader("Authorization", "Basic " + authStringEnc);
    HttpResponse response = httpClient.execute(request); 
    System.out.println("GET Response Status:: "
            + response.getStatusLine().getStatusCode());
    return resp;
}
}

Below Jars added in both class path and server lib :

  • httpclient-4.5.14
  • httpcore-4.4.15

Got ERROR like below

java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/TrustAllStrategy 

Update:

By using Java Decompiler, tried to extract Java code from httpclient jar in order to check whether TrustAllStrategy class present or not,

class

outcome: Class resides inside Jar, then what causes this issue, No idea on this.

May I know the way to resolve this ERROR?

0

There are 0 answers