I'm trying to make a RestClient, but I have an error from RestTemplate class and I don't know why.
@Component public class RestClient {
private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
private final RestTemplate restTemplate;
@Autowired
public RestClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public <T> T getForObject(
final String url,
final Map<String, String> headerParams,
final Class<T> expectedResponseType){
final HttpHeaders headers = new HttpHeaders();
headers.setAll(headerParams);
return exchange(url, expectedResponseType, new HttpEntity<>(headers), HttpMethod.GET);
}
private <T> T exchange(
final String url,
final Class<T> expectedResponseType,
final HttpEntity<Object> httpEntity,
final HttpMethod method) {
LOGGER.debug("Doing a {} on url {}", method, url);
return restTemplate.exchange(url, method, httpEntity, expectedResponseType).getBody();
}
}
and the error is this:
APPLICATION FAILED TO START
Description:
Parameter 0 of constructor in com.coffee.shop.starbux.cart.clients.RestClient required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
Process finished with exit code 0
CodePudding user response:
You have to provide a Bean of type RestTemplate
, e.g,
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
CodePudding user response:
please try like this below:
@Autowired
private RestTemplate restTemplate;