Search code examples
javaspring-bootmavenmicroservices

I'm trying to set up a service Order (microservice). but I get a error bean type constantly


img

So I'm trying for dear life to fix this issue with no proper fixes for it... each time I run Spring Boot I encounter this error, I have tried everything I have in power with no help. IDK if it's the compiler or me just going crazy.

package no.kristiania.orderservice.order.service;

import lombok.RequiredArgsConstructor;
import no.kristiania.orderservice.order.entity.Order;
import no.kristiania.orderservice.order.entity.OrderRequest;
import no.kristiania.orderservice.order.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@RequiredArgsConstructor
public class OrderServiceIMPL {

    private final OrderRepository orderRepository;
    private final RestTemplate restTemplate;

    @Value("${gateway.url}")
    private String gatewayUrl;

    public Order createOrder(OrderRequest orderRequest) {
        Order order = new Order();
        order.setCarId(orderRequest.getCarId());
        orderRepository.save(order);

        ResponseEntity<Boolean> response = restTemplate.getForEntity(gatewayUrl + "/api/products/" + order.getCarId() + "/availability", Boolean.class);
        boolean isCarAvailable = Boolean.TRUE.equals(response.getBody());

        if (!isCarAvailable) {
            // Handle scenario where car is not available
            return null;
        }

        return order;
    }
}

and the OrderConfig which my compiler has issues with

package no.kristiania.orderservice.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class OrderConfig {

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

I'm willing to try any suggestions so feel free to lecture me XD. Oh yeah almost forgot GitHub I'm using https://github.com/ahadius/MikroExam


Solution

  • Maybe is the way you are handling dependency injection. Try the following code, removing the @RequiredArgsConstructor and using the @Autowired annotations:

    @Service
    public class OrderServiceIMPL {
    
        @Autowired
        private OrderRepository orderRepository;
    
        @Autowired
        private RestTemplate restTemplate;