0

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Justin
  • 855
  • 2
  • 11
  • 30
  • The class is there in httpclient JAR, which you have included. Please check server classpath. If you are deploying as a WAR check if it is getting packaged in WEB-INF/lib. The class is very much there, so it is a classpath problem. To be doubly sure, open the jar file and physically check – Ironluca Jun 27 '23 at 16:11
  • Hi @Ironluca Thanks for reply, As you suggested, tried to check Jar which has class or not. class present there. please check updated question for more details. – Justin Jun 29 '23 at 04:03
  • What is your mode of deployment, is it a regular WAR file or is it part of a framework like Spring Boot. What is the server container you are using – Ironluca Jun 29 '23 at 10:21
  • Hi @Ironluca Actually i am integrating this piece of Java class mediation inside Integration tool called "WSO2 EI" where we can write java code as well. At finally we can export this class as Jar and place it inside wso2 server lib path or include this class mediator inside deployable archive file called "CAR" which is specific to WSO2 tool. – Justin Jun 29 '23 at 13:05

0 Answers0