WowBow
WowBow

Reputation: 7613

I wasn't able to autowire org.asynchttpclient.AsyncHttpClient in my Spring Web project

I have the following @RestController which makes POST request. I am able to Autowire and make REST calls with Resttemplate but when I try to use AsyncHttpClient, Spring complains about ClassNotFoundException even if I did autowire the class and provided the Bean in the same class.

I also have applicationl.properties file that allows circular for now reference(spring.main.allow-circular-references=true)

I am using Spring Boot start 2.7.8 and asnyc-http-client 2.12.3

I am not sure what I am missing here. May be the way I created the HttpClient isn't right ?

package com.example.demo.controller;

import java.io.IOException;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Component
public class WebhookListener {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        // Do any additional configuration here
        return builder.build();
    }

    @Bean
    public AsyncHttpClient asyncHttpClient() {
        // Do any additional configuration here
        return new DefaultAsyncHttpClient();
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AsyncHttpClient client;

    @PostMapping("/parasol/registerhook")
    public void createWebhook() {

        System.out.println("starting....");

        // AsyncHttpClient client = new DefaultAsyncHttpClient();
        client.prepare("POST", "https://alchemy-sdk-core-example.com/create-webhook")
                .setHeader("accept", "application/json")
                .setHeader("X-Alchemy-Token", "abc...333")
                .setHeader("content-type", "application/json")
                .setBody(
                        "{\"AddressWebhookParams\":{\"addresses\":[\"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D\",\"0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9\"],\"network\":\"ETH_MAINNET\"},\"url\":\"https://bda7-2600-1700-87d3-9200-edc9-c4f5-5ad9-1f14.ngrok.io/parasol/accountlistner\",\"type\":\"ADDRESS_ACTIVITY\"}")
                .execute()
                .toCompletableFuture()
                .thenAccept(System.out::println)
                .join();

        try {
            client.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Below is the exception:

ERROR 19879 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.propertySourcesPlaceholderConfigurer
        at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:193) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:153) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:343) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.8.jar:2.7.8]
        at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.example.demo.controller.WebhookListener] from ClassLoader [sun.misc.Launcher$AppClassLoader@15db9742]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:485) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:361) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:418) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:765) ~[spring-beans-5.3.25.jar:5.3.25]
        at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_292]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:764) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:703) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:674) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1684) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:570) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:250) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:243) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:233) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:181) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:156) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.7.8.jar:2.7.8]
        ... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/asynchttpclient/AsyncHttpClient
        at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_292]
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_292]
        at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_292]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.25.jar:5.3.25]
        ... 33 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.asynchttpclient.AsyncHttpClient
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_292]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_292]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_292]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_292]
        ... 37 common frames omitted

2023-02-05 20:07:10.382  WARN 19879 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

java.lang.IllegalStateException: Failed to introspect Class [com.example.demo.controller.WebhookListener] from ClassLoader [sun.misc.Launcher$AppClassLoader@15db9742]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:485) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:361) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:418) ~[spring-core-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:765) ~[spring-beans-5.3.25.jar:5.3.25]
        at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_292]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:764) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:703) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:674) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1684) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:570) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:669) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:661) ~[spring-beans-5.3.25.jar:5.3.25]
        at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1300) ~[spring-context-5.3.25.jar:5.3.25]
        at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:864) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:852) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:839) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:779) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.8.jar:2.7.8]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.8.jar:2.7.8]
        at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
Caused by: java.lang.NoClassDefFoundError: org/asynchttpclient/AsyncHttpClient
        at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_292]
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_292]
        at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_292]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.25.jar:5.3.25]
        ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.asynchttpclient.AsyncHttpClient
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_292]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_292]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352) ~[na:1.8.0_292]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_292]
        ... 25 common frames omitted

Upvotes: 0

Views: 364

Answers (1)

krishnan
krishnan

Reputation: 328

Looks like the AsyncHttpClient is not available in your classpath. Ensure your pom is free from typo asnyc-http-client(as in the question above).

<dependency>
    <groupId>org.asynchttpclient</groupId>
    <artifactId>async-http-client</artifactId>
    <version>2.2.0</version>
</dependency>

Upvotes: 1

Related Questions