Home > Software engineering >  Getting exception while trying to build SsmClient from aws-sdk-java version 2
Getting exception while trying to build SsmClient from aws-sdk-java version 2

Time:01-24

The exception I am getting --

java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultClientConnectionReuseStrategy
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76)
  at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212)
  at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:166)
  at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:393)
  at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:200)
  at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
  at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
  at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

The code block where exception is occurring --

//The region value being passed is "us-east-1" and paramKeyPath is the key present in aws systems //manager.
public String getParameterUsingAwsSSM(String paramKeyPath, String region) throws Exception {
        String paramKeyValue = null;
        System.out.println("Key path in AWS systems manager :: "   paramKeyPath);
        try {
            Region newRegion = Region.of(region);
                        //The below line gets printed---
            System.out.println("Region:: "   newRegion);
                        //Exception occurs in below line most probably---
            SsmClient ssmClient = SsmClient.builder().region(newRegion).build();
                        //The below line doesn't get printed---
            System.out.println("successfully got ssmclient");
            GetParameterRequest parameterRequest = GetParameterRequest.builder().name(paramKeyPath)
                    .withDecryption(Boolean.TRUE).build();
            System.out.println("successfully parameterRequest fetched");
            GetParameterResponse parameterResponse = ssmClient.getParameter(parameterRequest);
            System.out.println("successfully parameterResponse fetched");
            paramKeyValue = parameterResponse.parameter().value();
            System.out.println("The value of param is ::: " 
                    parameterResponse.parameter().value());
        } catch (Exception exception) {
            System.out.println("Exception from getParameterUsingAwsSSM() : "  exception);
            throw exception;
        }
        return paramKeyValue;
    }

The dependencies I have added in my pom.xml --

<dependencies>
        <dependency>
            <!-- this is an upgrade from 3.0-rc4 -->
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>cloudfront</artifactId>
            <version>2.19.15</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk18on</artifactId>
            <version>1.72</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk18on</artifactId>
            <version>1.72</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>regions</artifactId>
            <version>2.19.15</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>ssm</artifactId>
            <version>2.19.15</version>
        </dependency>
    </dependencies>

Can someone please help me with this? I have been stuck in this since 3 days.

I was expecting to get the parameter value from aws parameter store but I am getting the above mentioned exception, I tried adding org.apache.httpcomponents.httpclient dependency also to my project but still not working.

CodePudding user response:

I just tested the code here in AWS Github.

enter image description here

Try using the POM/code example located in this Git repo. Did you take your POM dependencies from this github repo?

CodePudding user response:

This is a change from V1 of the SDK. Add something like:

    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>url-connection-client</artifactId>
        <version>2.19.15</version>
    </dependency>

to your pom.xml.

  • Related