Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot.API GateWay, unable to access a service from the Spring boot GateWay

i am building a project where I have multiple micro-services..one of these services is the product service.I inserted an api GateWay so client requests go to the gateway first then to the specific service...but i am unable to access my product service or any other service after i hit the Gateway.

error message i get is

No static resource api/product" suggests that the API Gateway is looking for a static resource at the path "/api/product

Here is the application.poperties file for product service

spring.data.mongodb.uri=mongodb://localhost:27017/product-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
spring.application.name=product-service
server.port=0

Here is the application.poperties file for the APi GateWay

spring.cloud.gateway.routes[0].id=product-service
spring.cloud.gateway.routes[0].uri=lb://product-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/product

I have to mention that all services are registered in the Eureka Server including the Api Gateway

Additional note when i directly try to access the product service on its port i get a response back.

Here is the product controller for reference


@RestController///to expose a restful api
@RequestMapping("/api/product")
@RequiredArgsConstructor
public class ProductController {

    private  final ProductService productService;

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void createProduct(@RequestBody ProductRequest productRequest){
        productService.createProduct(productRequest);
    }

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<ProductResponse> getAllProducts() {
        return productService.getAllProducts();
    }
    
}

and here is the request i make (make it on Insomnia):

http://localhost:8080/api/product

i tried debugging in multiple ways i added logs but could understand why the exception itself occurred.

like image 614
majd Avatar asked Oct 14 '25 03:10

majd


1 Answers

Thanks guys, turned out that i had the dependency

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
    </dependency>

it should be

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

as the first is not compatible with all versions

like image 155
majd Avatar answered Oct 17 '25 23:10

majd