简体   繁体   中英

Parameter 0 of constructor in ... required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found

I can't run my application that I learn from this link ' https://developers.ascendcorp.com/สร้าง-microservices-ด้วย-netflix-oss-และ-spring-cloud-2678667d9dbc '

when I run application, it shows ' Parameter 0 of constructor in com.krittawat.productservice.controller.ProductController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found. ' in terminal.

my code : controller :

package com.krittawat.productservice.controller;

import com.krittawat.productservice.model.Product;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping(value = "products")
public class ProductController {
    private final RestTemplate restTemplate;

    @Autowired
    public ProductController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/search")
    public Product getProductsByTypeAndName(@RequestParam(value = "sku") final String sku) {
        String url = "http://PRICING-SERVICE/products/price?sku=" + sku;
        return restTemplate.getForObject(url, Product.class);
    }
}

model :

package com.krittawat.productservice.model;

import lombok.Data;

@Data
public class Product {
    private String sku;
    private String price;
}

main application:

@SpringBootApplication
@EnableDiscoveryClient
public class ProductServiceApplication {

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

}

Create a bean of Rest template in Configuration class as shown below

@Bean public RestTemplate restTemplate(){ return new RestTemplate(); }

then Autowire it wherever you want to use this.

@Autowired
private RestTemplate restTemplate;

Rest Template is used to create applications that consume RESTful Web Services. You should declare a Bean for Rest Template to auto wiring the Rest Template object:

@Configuration
public class RestClientConfiguration {

    // First Method: default
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    
    // Second Method: Using RestTemplateBuilder
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}

You can declare it also inside your SpringBootApplication class:

@SpringBootApplication
public class MyApplication {
   public static void main(String[] args) {
      SpringApplication.run(MyApplication.class, args);
   }
   @Bean
   public RestTemplate getRestTemplate() {
      return new RestTemplate();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM