Machhindra Neupane
Machhindra Neupane

Reputation: 727

org.springframework.beans.factory.UnsatisfiedDependencyException on Cloud Rest Client with Netflix Ribbon

I am getting a org.springframework.beans.factory.UnsatisfiedDependencyException while running SpringClientSideRibbonApplication.java class. I think there is no error but I don't know why this exception comes. Please help me. I am using STS IDE.

MyConfiguration.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.WeightedResponseTimeRule;
public class MyConfiguration {
@Autowired
IClientConfig ribbonClientConfig;   
@Bean
public IPing ribbonPing(IClientConfig config) {
    return new PingUrl();
}

@Bean
public IRule ribbonRule() {
  return new WeightedResponseTimeRule();
}


}

SpringClientSideRibbonApplication.java

package com.javasampleapproach.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

@SpringBootApplication
@RibbonClient(name = "helloworld", configuration = 
MyConfiguration.class)
public class SpringClientSideRibbonApplication {

public static void main(String[] args)
{
    SpringApplication.run(SpringClientSideRibbonApplication.class, 
args);
}
}

WebController.java

package com.javasampleapproach.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class WebController {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
    return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping("/helloworld")
public String home() {      
    return  
this.restTemplate.getForObject("http://helloworld/greeting", 
String.class);
}
}

Error Msg

Error creating bean with name 'myConfiguration': Unsatisfied 
dependency expressed through field 'ribbonClientConfig'; nested 
exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ribbonClientConfiguration': Invocation 
of init method failed; nested exception is 
java.lang.NoClassDefFoundError: 
org/apache/commons/configuration/AbstractConfiguration

Application.yml

spring:
application:
name: Ribbon-Client

helloworld:
ribbon:
eureka:
  enabled: false
listOfServers: localhost:8090,localhost:8091,localhost:8092
ServerListRefreshInterval: 15000

server:
port: 8080

Dependency

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
        <version>1.3.5.RELEASE </version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
   </dependencies>

Upvotes: 2

Views: 1089

Answers (1)

Machhindra Neupane
Machhindra Neupane

Reputation: 727

Their is no coding error. The problem was in my Maven dependency.My internet is slow so Maven download the corrupt jar file. I tried with Gradle. Now it's okey

Upvotes: 1

Related Questions