so ive been looking up on diffrent solutions for the problem, im using intelij idea, what im tryng to do is set up microservices where my order service communicates to product service via API-Gateway, later on im thinking to set opp rabbitmq for this. but ive stumbled across this issue. i dont understand what its not happy about.
package no.kristiania.orderservice.order.service;
import lombok.AllArgsConstructor;
import no.kristiania.orderservice.order.entity.Order;
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
@AllArgsConstructor
public class OrderServiceIMPL implements OrderService {
private final OrderRepository orderRepository;
private final RestTemplate restTemplate;
@Value("${gateway.url}")
private String gatewayUrl; // Changed to private - the issue is on gatewayUrl
@Override
public Order createOrder(Order order) {
// Check if the car is available by making a request to the product service through the gateway
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 (e.g., throw an exception or return null)
return null;
}
// Car is available, create the order
return orderRepository.save(order);
}
}
https://github.com/ahadius/MikroExam - github im using.
enter image description here
when i check value above the issue it does indeed get the port and localhost url of the gateway..
error i get when i try to run the springboot. enter image description here
You are using @AllArgsConstructor which generates the constructor with three arguments including gatewayUrl. But the @Value annotation is not propagated to the argument, that's why you see that error. Simply change @AllArgsConstructor to @RequiredArgsConstructor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With